Now here is a timed very SLOW read from file ... HLA example ... from a LARGE file of 3,000,000 random integers ...
(See example of the C code version at the bottom of the msortVecInt.hla program file).
Note: USING "vector.hhf" and "vector_func's.hhf' here ... You will first need to create a file of 3,000,000 random integers ... AND MAKE SURE there is NO WHITE SPACE/New line char's at the end of the file after the ending 3,000,000th integer ... (OR the program will CRASH - but not with C coded Cvec version ... and file read in C version was 50 to 90 times faster.)
/* msortVecInt.hla */ /* this version 2012-08-22 */
/*
NOTE!
FILE READ ERROR if NL char at end of file of integers ...
SO ... first MUST ensure all non-numeric char's, are trimmed from
the end of the file of integers ... can use a text editor !
*/
/*
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
program msortVecIntLarge;
const
InitCap: uns32 := 3000000; // to hold 3 Million random int's
HEADER: string := "HLA program to sort 3 Million random int's from file"
#13 #10 "stored in an HLA user defined type Vec...";
//#includeOnce( "readWord.hhf" )
type
Rec: record
rval: int32;
endrecord;
pRec: pointer to Rec;
procedure freeRec( pr: pRec in eax ); @noframe;
begin freeRec;
// NO dynamic memory here to free ...
ret();
end freeRec;
// ok ... after above two types defined ... now can ...
#includeOnce( "vector_func's.hhf" )
procedure myCmpInt( a: pRec in esi; b: pRec in edi ); @returns( "eax" );
begin myCmpInt;
mov( (type Rec [esi]).rval, eax );
if( eax <= (type Rec [edi]).rval ) then mov( 1, eax );
else mov( 0, eax );
endif;
end myCmpInt;
procedure isSorted( var v: Vec ); @returns( "eax" );
begin isSorted;
mov( v, ebx );
mov( (type Vec [ebx]).size, ecx );
dec( ecx );
intmul( @size( Rec ), ecx, eax );
add( (type Vec [ebx]).ary, eax );
mov( (type Rec [eax]).rval, edi );
while( (type int32 ecx) > 0 ) do
dec( ecx );
sub( @size( Rec ), eax );
mov( (type Rec [eax]).rval, esi );
if( (type int32 esi) > edi ) then
mov( 0, eax );
break;
endif;
mov( esi, edi );
endwhile;
mov( 1, eax );
end isSorted;
static
myVec: Vec;
myRec: Rec;
fin: dword;
tmpStr: string;
clock: timer_t;
begin msortVecIntLarge;
stdout.put( HEADER, nl nl );
initVec( myVec );
clock.create();
clock.start();
fileio.open( "randInt3M.txt", fileio.r );
mov( eax, fin );
while( !fileio.eof( fin ) ) do
fileio.geti32( fin );
mov( eax, myRec.rval );
// push( ebx ); push( ecx ); push( esi ); push( edi ); // if needed //
push_backVec( myVec, myRec );
endwhile;
clock.stop();
stdout.put( nl "To fill Vec from file ... ms = ", (type uns64 clock.Accumulated), nl );
clock.start();
mov( &myCmpInt, pMyCmp );
msortVec( myVec );
clock.stop();
stdout.put( nl "To msort ... ms = ", (type uns64 clock.Accumulated), nl );
isSorted( myVec );
stdout.put( "isSorted( myVec ) = ", (type boolean al) );
stdout.puts( nl nl "Press 'Enter' to exit/continue ... " );
stdin.readLn();
end msortVecIntLarge;
#if( false )
int main( void ) /* ***************************************************** */
{
FILE* fp;
double ti, tp;
int i, valToFind;
Rec r;
Cvec cv;
initCvec( &cv ); /* Note: MUST initial Cvec v ... for Cvec to work */
puts( HEADER );
ti = clock();
fp = fopen( "randInt3M.txt", "r" );
while( 1 == fscanf( fp, "%d", &r.val ) )
{
push_backCvec( &cv, &r ); /* since good Rec was obtained */
/*if( cv.size == 32768*3 ) break; // 32768*11 */
}
tp = clock() - ti;
printf( "%d int's were read from file in %.2f sec's ... ",
cv.size, tp/CLOCKS_PER_SEC );
ti = clock();
msort( &cv );
tp = clock() - ti;
printf( "merge sorted in %.2f sec's", tp/CLOCKS_PER_SEC );
printf( "\nand isSorted( &v ) = %d\n", isSorted( &cv ) );
ti = clock();
uniqueCvec( &cv );
tp = clock() - ti;
printf( "\nuniqueCvec in %.2f sec's", tp/CLOCKS_PER_SEC );
printf( " ... and isSorted( &cv ) = %d ", isSorted( &cv ) );
printf( "\n... and isUniqueCvec( &cv ) = %d\n",
isUniqueCvec( &cv ) & cv.isSorted );
printf( "\nAfter uniqueCvec: cv.size = %d, cv.cap = %d\n\n", cv.size, cv.cap );
valToFind = cv.ary[cv.size-1].val;
ti = clock();
i = findCvec( &cv, valToFind );
if( i != -1 )
{
eraseCvec( &cv, i );
tp = clock() - ti;
printf( "%d was erased ...", valToFind);
printf( "%d is new end val...\n", cv.ary[cv.size-1].val);
printf( "find/ereaseCvec in %.2f sec's\n\n", tp/CLOCKS_PER_SEC );
}
else printf( "%d was NOT-found/NOT-erased ...\n\n", valToFind );
myShowAll( &cv );
printf( "Before clearCvec: cv.size = %d, cv.cap = %d\n", cv.size, cv.cap );
clearCvec( &cv );
printf( "After clearCvec: cv.size = %d, cv.cap = %d\n", cv.size, cv.cap );
fclose( fp );
fputs( "\nPress 'Enter' to continue ... ", stdout );
getchar();
return 0;
} /* ******************************************************************** */
#endif
You will find the 2 files you need to have available to include ...
"vector_func's.hhf"
"vector.hhf"
at this next link:
http://developers-heaven.net/forum/index.php?topic=2600.0Here is a speeded up version that reads a line at a time ... (also handles spaces at end of lines)
/* msortVecInt2.hla */ /* this version 2012-08-22 */
/*
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
program msortVecIntLarge;
const
InitCap: uns32 := 3_000_000; // to hold 3 Million random int's
HEADER: string := "HLA program to sort 3 Million random int's from file"
#13 #10 "stored in an HLA user defined type Vec...";
type
Rec: record
rval: int32;
endrecord;
pRec: pointer to Rec;
procedure freeRec( pr: pRec in eax ); @noframe;
begin freeRec;
// NO dynamic memory here to free ...
ret();
end freeRec;
// ok ... after above two types defined ... now can ...
#includeOnce( "vector_func's.hhf" )
procedure myCmpInt( a: pRec in esi; b: pRec in edi ); @returns( "eax" );
begin myCmpInt;
mov( (type Rec [esi]).rval, eax );
if( eax <= (type Rec [edi]).rval ) then mov( 1, eax );
else mov( 0, eax );
endif;
end myCmpInt;
procedure isSorted( var v: Vec ); @returns( "eax" );
begin isSorted;
mov( v, ebx );
mov( (type Vec [ebx]).size, ecx );
dec( ecx );
intmul( @size( Rec ), ecx, eax );
add( (type Vec [ebx]).ary, eax );
mov( (type Rec [eax]).rval, edi );
while( (type int32 ecx) > 0 ) do
dec( ecx );
sub( @size( Rec ), eax );
mov( (type Rec [eax]).rval, esi );
if( (type int32 esi) > edi ) then
mov( 0, eax );
break;
endif;
mov( esi, edi );
endwhile;
mov( 1, eax );
end isSorted;
static
myVec: Vec;
myRec: Rec;
fin: dword;
tmpStr: string;
clock: timer_t;
line: str.strvar( 256 ); // lines here are less than 100 char's
begin msortVecIntLarge;
stdout.put( HEADER, nl nl );
initVec( myVec );
clock.create();
clock.start();
fileio.open( "randInt3M.txt", fileio.r );
mov( eax, fin );
while( !fileio.eof( fin ) ) do
fileio.gets( fin, line ); // max size fixed above to 256 char's
mov( line, esi );
mov( (type str.strRec [esi]).length, edx );
if( edx ) then // i.e. if line NOT empty ... trim any spaces from end
dec( edx );
while( (type char [esi+edx]) == ' ' ) do dec( edx ); endwhile;
inc( edx );
mov( 0, (type char [esi+edx]) ); // ensure 0 terminated
//mov( edx, (type str.strRec [esi]).length ); // update with new length
while( (type char [esi]) != 0 ) do
conv.atoi32( [esi] );
mov( eax, myRec.rval );
// push( ebx ); push( ecx ); push( esi ); push( edi ); // if needed //
push( esi );
push_backVec( myVec, myRec );
pop( esi );
endwhile;
endif;
endwhile;
clock.stop();
stdout.put( nl "To fill Vec from file ... ms = ", (type uns64 clock.Accumulated), nl );
stdout.put( "myVec.size = ", myVec.size,
", myVec.capacity = ", myVec.capacity, nl );
clock.start();
mov( &myCmpInt, pMyCmp );
msortVec( myVec );
clock.stop();
stdout.put( nl "To msort ... ms = ", (type uns64 clock.Accumulated), nl );
isSorted( myVec );
stdout.put( "isSorted( myVec ) = ", (type boolean al), nl );
stdout.put( "myVec.size = ", myVec.size,
", myVec.capacity = ", myVec.capacity, nl );
clearVec( myVec );
stdout.puts( nl "Press 'Enter' to exit/continue ... " );
stdin.readLn();
end msortVecIntLarge;
Now ... a super fast load from file, reading the whole file into one large dynamic buffer in ONE big gulp ... (also handles NON numeric/whitespace at end of file, if any to handle) ...
/* msortVecInt3.hla */ /* this version 2012-08-22 */
/*
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
program msortVecIntLarge;
const
InitCap: uns32 := 3_000_000; // to hold 3 Million random int's
FNAME: string := "randInt3M.txt";
HEADER: string := "HLA program to sort 3 Million random int's from file"
#13 #10 "stored in an HLA user defined type Vec...";
type
Rec: record
rval: int32;
endrecord;
pRec: pointer to Rec;
procedure freeRec( pr: pRec in eax ); @noframe;
begin freeRec;
// NO dynamic memory here to free ...
ret();
end freeRec;
// ok ... after above two types defined ... now can ...
#includeOnce( "vector_func's.hhf" )
procedure myCmpInt( a: pRec in esi; b: pRec in edi ); @returns( "eax" );
begin myCmpInt;
mov( (type Rec [esi]).rval, eax );
if( eax <= (type Rec [edi]).rval ) then mov( 1, eax );
else mov( 0, eax );
endif;
end myCmpInt;
procedure isSorted( var v: Vec ); @returns( "eax" );
begin isSorted;
mov( v, ebx );
mov( (type Vec [ebx]).size, ecx );
dec( ecx );
intmul( @size( Rec ), ecx, eax );
add( (type Vec [ebx]).ary, eax );
mov( (type Rec [eax]).rval, edi );
while( (type int32 ecx) > 0 ) do
dec( ecx );
sub( @size( Rec ), eax );
mov( (type Rec [eax]).rval, esi );
if( (type int32 esi) > edi ) then
mov( 0, eax );
break;
endif;
mov( esi, edi );
endwhile;
mov( 1, eax );
end isSorted;
static
myVec: Vec;
myRec: Rec;
fin: dword;
tmpStr: string;
clock: timer_t;
buf: pointer to char;
begin msortVecIntLarge;
stdout.put( HEADER, nl nl );
initVec( myVec );
clock.create();
clock.start();
fileio.open( FNAME, fileio.r );
mov( eax, fin );
filesys.size( fin );
mov( eax, ecx );
inc( eax ); // leave room for terminal 0
mem.alloc( eax );
mov( eax, buf );
fileio.read( fin, [eax], ecx );
if( eax != -1 ) then
if( ecx ) then
dec( ecx );
mov( buf, esi );
while( (type int32 ecx) >= 0 && (type char [esi+ecx]) < '0' ||
(type char [esi+ecx]) > '9' ) do dec( ecx );
endwhile;
inc( ecx );
mov( 0, (type char [esi+ecx]) ); // ensure 0 terminated
while( (type char [esi]) != 0 ) do
conv.atoi32( [esi] );
mov( eax, myRec.rval );
// push( ebx ); push( ecx ); push( esi ); push( edi ); // if needed //
push( esi );
push_backVec( myVec, myRec );
pop( esi );
endwhile;
endif;
else
stdout.put( "There was a problem reading file ", FNAME, nl );
endif;
mem.free( buf );
clock.stop();
stdout.put( nl "To fill Vec from file ... ms = ", (type uns64 clock.Accumulated), nl );
stdout.put( "myVec.size = ", myVec.size,
", myVec.capacity = ", myVec.capacity, nl );
clock.start();
mov( &myCmpInt, pMyCmp );
msortVec( myVec );
clock.stop();
stdout.put( nl "To msort ... ms = ", (type uns64 clock.Accumulated), nl );
isSorted( myVec );
stdout.put( "isSorted( myVec ) = ", (type boolean al), nl );
stdout.put( "myVec.size = ", myVec.size,
", myVec.capacity = ", myVec.capacity, nl );
clearVec( myVec );
stdout.puts( nl "Press 'Enter' to exit/continue ... " );
stdin.readLn();
end msortVecIntLarge;