Re: getc( ) and gets( ) speed

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hi Derek,

How about this example, if there is a way to incorporate this
into win32lib for reading Ascii files where each line is identical
in length, not much would need change except for the line length
count and add 2 to that variable.

BTW, this example is faster than using getc( ) and gets( )

If you need the code because of line wrap let me know...

-- Co-Authors H.W Overman and Tommy Carlier
-- Dec 15, 2003

include machine.e
include file.e
include dll.e

atom t1, t2, fn, lFileLen, result
sequence lFile, line

atom kernel32
kernel32 = open_dll("kernel32.dll")

constant
xCreateFile =
define_c_func(kernel32,"CreateFileA",{C_POINTER,C_LONG,C_LONG,C_POINTER,C_LONG,C_LONG,C_INT},C_LONG),
xReadFile =
define_c_func(kernel32,"ReadFile",{C_INT,C_POINTER,C_UINT,C_POINTER,C_POINTER},C_LONG)

global constant 
  GENERIC_READ             = #80000000,
  FILE_ATTRIBUTE_NORMAL    = #80,
  FILE_FLAG_SEQUENTIAL_SCAN= #8000000,
  OPEN_EXISTING            = 3

atom hFile
  
global function CreateFile(sequence fname)
atom FileName 
     FileName = allocate_string(fname)
     hFile = c_func(xCreateFile,{FileName,
                                GENERIC_READ,
                                0,
                                NULL,
                                OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL+FILE_FLAG_SEQUENTIAL_SCAN,
                                NULL})
    return hFile
end function

atom lpNumberOfBytesRead
lpNumberOfBytesRead = allocate(4) --lpNumberOfBytesRead

function ReadFile(atom hFile, atom lpBuffer, atom nNumberOfBytesToRead)
return
  c_func(xReadFile,{hFile,lpBuffer,nNumberOfBytesToRead,lpNumberOfBytesRead,0})
end function

fn = open("trmdemo.txt", "r")
lFileLen = seek(fn, -1)                                 
lFileLen = where(fn)
close(fn)

atom lpFileBuff, cIN, cIndex, cMaxIndex
lpFileBuff = allocate(lFileLen)
cIN = 0

hFile = CreateFile("trmdemo.txt")
result = ReadFile(hFile, lpFileBuff, lFileLen) 

if result then
   t1 = time()   
   cMaxIndex = lpFileBuff + lFileLen
   cIndex = lpFileBuff
   while cIndex < cMaxIndex do
      line = peek({cIndex, 200})
      cIndex += 202
   end while
   t2 = time()- t1  
end if                                  
printf(1,"Average Time : %1.4f sec\n", t2 )


if getc(0) then end if

-- Euman 2003

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu