Page 1 of 1 1
Topic Options
#212619 - 2017-08-14 04:15 AM ReadLine()
AndreLuiz Offline
Getting the hang of it

Registered: 2015-10-07
Posts: 89
Loc: Brasil, João pessoa
[ptbr]
Olá a todos, eu vim até aqui para compartilhar algo que me incomodou bastante.
É o seguinte, eu rodei um while, para capturar os dados de um arquivo com 4,419
linhas, e nesse momento ele levou 4s e 159ms, e para o que preciso é uma enorme tristeza.
Enfim ai eu resolvi, testar uma coisa nova, usar o wshpipe() para ver a velocidade. então ele levou uns 344ms, podem testar se quiser.

Conclusão se o arquivo for grande, melhor usar o wshpipe para isso.

[eng(TRANSLATE)]
Hello everyone, I came here to share something that bothered me a lot.
This is the following, I ran a while, to capture data from a file with 4,419 lines, and at that point it took 4s and 159ms, and what I need is a huge sadness.
Anyway, I decided to test something new, use wshpipe() to see the speed. So it took about 344ms, you can test if you want.

Conclusion If the file is large, best use wshpipe for this.

Codes:
 Code:
/* READLINE() */
$handle=freefilehandle()
$=Open($handle, $Dir)
$Dir = ''
while (@error = 0)
$Dir = $Dir + ReadLine($handle) + @crlf
loop
$=Close($handle)
$Dir = Split($Dir, @crlf)


 Code:
$Dir = Wshpipe('%comspec% /c Type "$Dir"', 1)


[ptbr]
Não sei se mais alguém já sabia disso, mas é sempre bom trazer essas informações.
Acho que vou poder na área de sugestões, algo do tipo, adicionar um ReadAll($handle, @crlf).
Onde o primeiro parâmetro seria o id, e o segundo parâmetro seria o delimitador para as linhas.

[eng(TRANSLATE)]
I do not know if anyone else already knew this, but it's always good to bring this information.
I think I'll be able to in the area of suggestions, something like, add a ReadAll($handle, @crlf).
Where the first parameter would be the id, and the second parameter would be the delimiter for the rows.

Top
#212624 - 2017-08-14 09:47 AM Re: ReadLine() [Re: AndreLuiz]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
You can use the File System Object to make use of ReadAll functionality:
 Code:
Dim $ForReading, $ForWriting, $ForAppending
$ForReading = 1
$ForWriting = 2
$ForAppending = 8
Dim $TriStateUseDefault, $TriStateTrue, $TriStateFalse
$TriStateUseDefault = -2	;-2 Opens the file using the system default
$TriStateTrue = -1		;-1 Opens the file as Unicode
$TriStateFalse = 0		;0 Opens the file as ASCII

Dim $strFile, $objFSO, $objFile, $objFileContent
$strFile = "C:\Windows\system32\drivers\etc\hosts"
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile($File, $ForReading, $TriStateTrue)
$objFileContent = $objFile.ReadAll
$=MessageBox($objFileContent,"Info")
$objFile.Close

Top
#212627 - 2017-08-15 04:05 PM Re: ReadLine() [Re: AndreLuiz]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
I suggest you an other solution which is fully KIXTART.
 Code:
$filename = "xxxxxxxx.txt"

dim $start, $start1
$start = @ticks
$dir = ReadFileToArray( $filename )
$start = @ticks - $start

if not @error
	$start1 = @ticks
	for each $line in $dir
		$line ?
	next
	$start1 = @ticks - $start1
endif

"file read in      " $start " ms" ?
"file displayed in " $start1 " ms" ?


function ReadFileToArray( $filename )
	dim $handle, $nbmax

	$nbmax = 4095
	redim $ReadFileToArray[$nbmax]

	$handle=freefilehandle()
	if Open($handle, $filename)<>0	exit 2	endif

	dim $line, $i
	$i = -1
	$line = Readline($handle)
	while not @error
		$i = $i + 1
		if $i > $nbmax
			$nbmax = $nbmax*2+1
			redim preserve $ReadFileToArray[$nbmax]
		endif
		$ReadFileToArray[$i] = $line

		$line = Readline($handle)
	loop
	$=Close($handle)

	if $i>-1
		redim preserve $ReadFileToArray[$i]
		exit 0
	else
		exit 1
	endif
endfunction
I used a file of 233KB with 4488 lines and the script took 31 ms to run (faster than string concatenation and than wshpipe) !!!
With a 11200 lines file (1861KB), the script runs in 110 ms.
My computer has an Intel Core I5 4200M (3000 Mhz) with 4 GB ram

in fact, using string concatenation is a bad idea because this is basically a slow operation. And may be there is a limitation on string size.
Using directly an array is faster even if the function has to resize the array several times ( with nbmax=0, the script run in 109 ms).
_________________________
Christophe

Top
#212628 - 2017-08-15 10:29 PM Re: ReadLine() [Re: ChristopheM]
AndreLuiz Offline
Getting the hang of it

Registered: 2015-10-07
Posts: 89
Loc: Brasil, João pessoa
[ptbr]
Opa obrigado, dessa eu não sabia, vlw mesmo!!
Mas mesmo assim usando objeto, é ainda mais rápido.

[eng(translate)]
Hi, thanks, I did not know that, vlw !!
But even so using object, it's even faster.

file read in (Kix) 641 ms
file read in (obj) 203 ms


 Code:
dim $start, $start1
$start = @ticks
$dir = ReadFileToArray( $filename )
$start = @ticks - $start


$start1	= @ticks
$objFSO	= CreateObject("Scripting.FileSystemObject")
$objFile	= $objFSO.OpenTextFile($filename, 1, 0)
$filename= Split($objFile.ReadAll, @crlf)
$objFile.Close
$start1	= @ticks-$start1


[ptbr]
O arquivo possuí 30.058 linhas, 464kb.
Enfim muito obrigado aos dois, foi muito valioso isso.

[eng(translate)]
The file has 30,058 lines, 464kb.
Anyway, thank you very much, it was very valuable.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.05 seconds in which 0.022 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org