Jump to content

New FAST line counter


ryeguy
 Share

Recommended Posts

I adjusted my version to the latest AutoIt to not leave carriage returns behind.

Func _NFileCountLines($sFile)
   Local $AArray
   Local $Content
   Local $FileSize
   Local $CountLines
   If Not FileExists($sFile) Then
      $CountLines = 0
   Else
      $FileSize = FileGetSize($sFile)
      $Content = FileRead($sFile, $FileSize)
      
      $AArray = StringSplit($Content, @CRLF, 1)
      $Content = 0
      $CountLines = $AArray[0]
   EndIf
   Return $CountLines
EndFunc
Edited by Jos
Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hehe, check out my contribution - at least 10 times faster than any of the others!

JdeB, could you try it with your test file?

Ps: File must have CRLF line-ends.

$TestFile = $CmdLine[1]

$StartTime = TimerInit()
$CountLines_3 = _NFileCountLines($TestFile) & " lines in "
$Test3_Time = Round(TimerDiff($StartTime) / 1000, 3) & " seconds"
$StartTime = TimerInit()
$CountLines_4 = _TylosFileCountLines($TestFile) & " lines in "
$Test4_Time = Round(TimerDiff($StartTime) / 1000, 3) & " seconds"
MsgBox(64, "Debug Test", "_NFileCountLines counted " & $CountLines_3 & $Test3_Time & @CRLF _
                       & "_TylosFileCountLines counted " & $CountLines_4 & $Test4_Time)
Exit
                       
Func _NFileCountLines($sFile)
  Local $AArray
  Local $Content
  Local $FileSize
  Local $CountLines
  If Not FileExists($sFile) Then
     $CountLines = 0
  Else
     $FileSize = FileGetSize($sFile)
     $Content = FileRead($sFile, $FileSize)    
     $AArray = StringSplit($Content, @CRLF, 1)
     $Content = 0
     $CountLines = $AArray[0]
  EndIf
  Return $CountLines
EndFunc


Func _TylosFileCountLines($file)
   If Not FileExists($file) Then
     SetError(1)
     Return 0
   EndIf
   Local $n = FileGetSize($file)
   Return $n - StringLen(StringStripCR(FileRead($file, $n))) + 1
EndFunc
Edited by Jos

blub

Link to comment
Share on other sites

Actually, my suggestion will count single @CR as lines, so it's not perfect. However, the following counts @LF's which is better (works also on UNIX files):

I tested a file with about 340000 lines (each about 9 chars long).

Jdeb's used about 1.77 seconds, whereas this used only 0.11 seconds:

Func _TylosFileCountLines($file)
  If Not FileExists($file) Then
    SetError(1)
    Return 0
  EndIf
  Local $n = FileGetSize($file)
  Return StringLen(StringAddCR(FileRead($file, $n))) - $n + 1
EndFunc
Edited by Jos

blub

Link to comment
Share on other sites

  • Developers

Hehe, check out my contribution - at least 10 times faster than any of the others!

JdeB, could you try it with your test file?

Ps: File must have CRLF line-ends.

We have a new winner !!!

Tylo's LineCount: counted 10000 lines in 0.843 seconds

_FileCountLines: counted 10000 lines in 4.034 seconds

_NFileCountLines: counted 10000 lines in 3.149 seconds

Internal record counter: counted 10000 lines in 1.024 seconds

Clever solution ! :idiot:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Tylo, it's great if yours works really quick, can you explain what it does, cos I can't understand it!

<{POST_SNAPBACK}>

I commented a little. I hope you'll understand now.

Func _TylosFileCountLines($file)
   If Not FileExists($file) Then
      SetError(1)
      Return 0
   EndIf
   Local $n = FileGetSize($file)
  ;1. File is read completely
  ;2. StringAddCR adds a carriage return to every line feed. Function: for every new line, a character is added.
  ;3. StringLen counts how many characters; There's a new total.
  ;4. From the new total the original file size is subtracted.
  ;5. A number of extra line feeds is left behind and that number will be returned.
  ;I'm not sure why the number 1 is added to the total...
   Return StringLen(StringAddCR(FileRead($file, $n))) - $n + 1
EndFunc
Edited by Jos
Link to comment
Share on other sites

Thanks :idiot: Here's the new library function. I think it is correct only to add 1 if the last char is different from @LF. Meaning, a trailing @LF should not count as a new line.

; Returns number of lines (separated by @LF's) in a file.
; If file could not be opened, return 0 and @error = 1
; How: Adds a @CR before each @LF, and computes num of @CR's that was added.
; Note: It does not count a final @LF as a line.

Func _FileCountLines( $sFilePath )
   Local $N = FileGetSize($sFilePath) - 1
   If @error Or $N = -1 Then Return 0
   Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc
Edited by Jos

blub

Link to comment
Share on other sites

Thanks  :D  Here's the new library function. I think it is correct only to add 1 if the last char is different from @LF. Meaning, a trailing @LF should not count as a new line.

; Returns number of lines (separated by @LF's) in a file.
; If file could not be opened, return 0 and @error = 1
; How: Adds a @CR before each @LF, and computes num of @CR's that was added.
; Note: It does not count a final @LF as a line.

Func _FileCountLines( $sFilePath )
   Local $N = FileGetSize($sFilePath) - 1
   If @error Or $N = -1 Then Return 0
   Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc

<{POST_SNAPBACK}>

Very good. :idiot:

@tylo: You left obsolete my only one(_FileCountLines) contributed to the AutoIt's Library.

I am glad that there are better alternatives. ;)

:lol:

Link to comment
Share on other sites

  • 2 months later...
Guest Guidosoft

Very good. :lmao:

@tylo: You left obsolete my only one(_FileCountLines) contributed to the AutoIt's Library.

I am glad that there are better alternatives.  :)

o:)

<{POST_SNAPBACK}>

So how does it work?
Link to comment
Share on other sites

  • Developers

So how does it work?

<{POST_SNAPBACK}>

as documented above:

; How: Adds a @CR before each @LF, and computes num of @CR's that was added.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

i can make clever beeps to but i usually am to lazy :lmao:

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Link to comment
Share on other sites

  • 5 years later...

Great code here, I know the post is old but it's still a great function :x

Getting the line count for me on a list of 151671 names was almost 2 seconds(without this function), but with this Function it's less than a second only 0.334

I always like to tweak my codes as much as possible, I don't like the lazy coders out there who produce programs that eat CPU & Memory! If a program does the same thing but eats less resources or takes for ever to start, guess which one I will pick :P

Edited by ALFJ
Link to comment
Share on other sites

I guess my measly .023 seconds for 10,000 lines or .21 seconds for 88,711 lines is out of the running here.

SRE's people, SRE's

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

@George,

just for clarity, this thread was created waaayyyyyyy before SRE was implemented in AutoIt3. :x

Having said that, we should probably look at the current UDF's for improvements using SRE.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@George,

just for clarity, this thread was created waaayyyyyyy before SRE was implemented in AutoIt3. :x

Having said that, we should probably look at the current UDF's for improvements using SRE.

Jos

Agreed Jos. That may well become an extension for the current help file documentation group once it's all caught up to date.

By the way, The function is here and it's _File_CountLines2(). I haven't made the changes to make it work with a string input like I did with my original _File_CountLines() which is in the same UDF. It does have the advantage of being able to ignore blank lines though which is something I didn't implement on the original.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...