kcvinu

How to find how many @LFs in a given text

40 posts in this topic

Hi all,

I am in the midle of a string related script. I need to find how many line breakes are there in the given text.  How can i do that. I think i need to use StringRegExp function. Am i right ?


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites



kcvinu,

No. Just use StringReplace and see what is returned in @extended: :)

#include <MsgBoxConstants.au3>

$sString = "Line 1" & @LF & "Line 2" & @LF & "Line 3"

StringReplace($sString, @LF, "")

MsgBox($MB_SYSTEMMODAL, "Count", @extended)
M23
1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

MsgBox($MB_SYSTEMMODAL, "Count", StringSplit($sString, @LF)[0]-1)

:)

Edit

of course if you absolutely want a regex...

MsgBox($MB_SYSTEMMODAL, "Count", UBound(StringRegExp($sString, @LF, 3)))
Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Or...

$sString = "Line 1" & @LF & "Line 2" & @LF & "Line 3"

MsgBox(0, 0, StringLen(StringRegExpReplace($sString, "[^\n]", ""))) ; replace everything that is not a linefeed by an empty string, then shows the length of the resulting string
Edited by SadBunny
1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

And another way.

#include <MsgBoxConstants.au3>

; Based on http://www.autoitscript.com/forum/topic/6330-new-fast-line-counter/page__view__findpost__p__44449

;For file
;$file = "Lines.txt"
;Local $N = FileGetSize($file)
;MsgBox($MB_SYSTEMMODAL, "Count", StringLen(StringAddCR(FileRead($file, $N))) - $N)

;For a string
;$sString = FileRead($file)
$sString = "Line 1" & @CRLF & "Line 2" & @LF & "Line 3"
MsgBox($MB_SYSTEMMODAL, "Count", StringLen(StringAddCR($sString)) - StringLen($sString))
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I like Melba23's way,
anyway, since some other worse attempt has been posted,
here is another even worst way ..... :P

#include <array.au3>
$sString = "Line 1" & @LF & "Line 2" & @LF & "Line 3"

MsgBox(0, "nr. of @LF", UBound(_ArrayFindAll(StringSplit($sString, "", 2), Chr(10))))
Edited by Chimp
1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

After Chimp's post, I realized that most people would not think that the StringAddCR method is the fastest.
Here are my results.

Speed (secs)    Poster      Method
0.32268            Malkey      StringLen(StringAddCR($sString)) - StringLen($sString)
0.55071            Mikell        UBound(StringRegExp($sString, @LF, 3))
0.76448            Mikell        StringSplit($sString, @LF)[0]-1
2.67618            Melba23   StringReplace($sString, @LF, "");  @extended
6.46621            SadBunny StringLen(StringRegExpReplace($sString, "[^n]", ""))
Crashed           Chimp        _ArrayFindAll(StringSplit($sString, "", 2), Chr(10))

The surprises for me was  Mikell's StringRegExp was comparatively fast but can be logically justified, being a simple RE pattern.

And, Melba23's StringReplace - @extended was comparatively slow.

#include <array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Local Const $sFilePath = @ScriptDir & "\TestFileWrite.txt"

; If file does not exist, create 25mb "TestFileWrite.txt" file.
If FileExists($sFilePath) = 0 Then
    Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf
    For $i = 1 To 1000000
        FileWrite($hFileOpen, "This line is line " & $i & @CRLF)
    Next
    FileClose($hFileOpen)
EndIf

$sString = FileRead($sFilePath)
Local $hTimer = TimerInit()

;$a = _ArrayFindAll(StringSplit($sString, "", 2), Chr(10)) ; Crashed
;MsgBox(0, "Chimp", "Time: " & TimerDiff($hTimer)/1000 " Secs" & @CRLF &  UBound($a)

$a = StringLen(StringAddCR($sString)) - StringLen($sString)
MsgBox(0, "Malkey String", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a) ; 0.32268 secs

;$N = FileGetSize($sFilePath)
;$a = StringLen(StringAddCR(FileRead($sFilePath))) - $N ; 0.69827 secs
;$a = StringLen(StringAddCR($sString)) - $N             ; 0.32333 secs
;MsgBox(0, "Malkey File ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a)

;$a =  StringLen(StringRegExpReplace($sString, "[^\n]", ""))
;MsgBox(0, "SadBunny ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a) ; 6.46621 secs

;$a =  UBound(StringRegExp($sString, @LF, 3))
;MsgBox(0, "Mikell II ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a) ; 0.55071 secs

;$a =  StringSplit($sString, @LF)[0]-1
;MsgBox(0, "Mikell I ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a) ; 0.76448 secs

;StringReplace($sString, @LF, "")
;$a = @extended
;MsgBox(0, "Melba23 ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a) ; 2.67618 secs

;FileDelete($sFilePath)

Share this post


Link to post
Share on other sites

How does this compare on your computer?

$hTimer = TimerInit()
StringRegExpReplace($sString, "\n", "\n")
$a =  @extended
MsgBox(0, "jchd ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

 

How does this compare on your computer?

$hTimer = TimerInit()
StringRegExpReplace($sString, "\n", "\n")
$a =  @extended
MsgBox(0, "jchd ", "Time: " & Round(TimerDiff($hTimer) / 1000, 5) & " secs" & @CRLF & $a)

The fastest speed after several runs was 0.36513 secs. Making it a close second fastest on my Environment(Language:0409  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64), running 3.3.12.0 AutoIt version.

Using

StringRegExpReplace($sString, "\n", "")

The fastest speed after several runs was 0.32308 secs.

This is getting very close to the speed of the StringAddCR method

Edit The StringAddCR method is based on:

'?do=embed' frameborder='0' data-embedContent>>

Edit2:  I did give this reference in post #5.

 

Edited by Malkey

Share this post


Link to post
Share on other sites

my post >#6 was obviously a joke, as stated by the sentence "here is another even worst way"
indeed is the worst of all

anyway are very interesting the surprising performances of the StringAddCR !!

here a time comparison "live"

#include <array.au3>

Local $sString, $hTimer, $aCompetitors[7][2] = [ _
        ["Melba23   ", 0], _
        ["Mikell    ", 0], _
        ["Mikell 2  ", 0], _
        ["SadBunny  ", 0], _
        ["Malkey    ", 0], _
        ["Chimp     ", 0], _
        ["jchd      ", 0]]

For $i = 1 To 100000
    $sString &= "This line is line " & $i & @CRLF
Next
$hTimer = TimerInit()

StringReplace($sString, @LF, "")
$a = @extended
Stopwatch(0, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[0][0]) & @TAB & $aCompetitors[0][1] & " secs" & @TAB & $a & @CRLF)

$a = StringSplit($sString, @LF)[0] - 1
Stopwatch(1, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[1][0]) & @TAB & $aCompetitors[1][1] & " secs" & @TAB & $a & @CRLF)

$a = UBound(StringRegExp($sString, @LF, 3))
Stopwatch(2, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[2][0]) & @TAB & $aCompetitors[2][1] & " secs" & @TAB & $a & @CRLF)

$a = StringLen(StringRegExpReplace($sString, "[^\n]", ""))
Stopwatch(3, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[3][0]) & @TAB & $aCompetitors[3][1] & " secs" & @TAB & $a & @CRLF)

$a = StringLen(StringAddCR($sString)) - StringLen($sString)
Stopwatch(4, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[4][0]) & @TAB & $aCompetitors[4][1] & " secs" & @TAB & $a & @CRLF)

$a = UBound(_ArrayFindAll(StringSplit($sString, "", 2), Chr(10))) ; Crashed
Stopwatch(5, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[5][0]) & @TAB & $aCompetitors[5][1] & " secs" & @TAB & $a & @CRLF)

StringRegExpReplace($sString, "\n", "\n")
$a = @extended
Stopwatch(6, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[6][0]) & @TAB & $aCompetitors[6][1] & " secs" & @TAB & $a & @CRLF)

_ArraySort($aCompetitors, 0, 0, 0, 1)
_ArrayDisplay($aCompetitors, "Results")

Func Stopwatch($iCompetitor, ByRef $hTimer, ByRef $aCompetitors)
    $aCompetitors[$iCompetitor][1] = Round(TimerDiff($hTimer) / 1000, 5)
    $hTimer = TimerInit()
EndFunc   ;==>Stopwatch


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Senseless unless the string is very long and also not really AutoIt but…

#include <Memory.au3>

Func StringCountLF($sString)
    Local $pCountLF = _MemVirtualAlloc(Null, 25, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)

    DllStructSetData(DllStructCreate("BYTE[25]", $pCountLF), 1, "0x" _
            & "8B4C2404" _     ;~     mov  ecx, dword[esp+04h]
            & "31C0"     _     ;~     xor  eax, eax
            & "8A11"     _     ;~  @@:mov  dl, byte[ecx]
            & "80FA00"   _     ;~     cmp  dl, 00h
            & "7409"     _     ;~     je   @f
            & "41"       _     ;~     inc  ecx
            & "80FA0A"   _     ;~     cmp  dl, 0Ah
            & "75F3"     _     ;~     jne  @b
            & "40"       _     ;~     inc  eax
            & "EBF0"     _     ;~     jmp  @b
            & "C20400"   _     ;~  @@:ret  4
    )

    Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $sString)[0]

    _MemVirtualFree($pCountLF, 0, $MEM_RELEASE)

    Return $nResult
EndFunc
Edited by Tekk
1 person likes this

Share this post


Link to post
Share on other sites

 

Senseless unless the string is very long and also not really AutoIt but…

#include <Memory.au3>

Func StringCountLF($sString)
    Local $pCountLF = _MemVirtualAlloc(Null, 25, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)

    DllStructSetData(DllStructCreate("BYTE[25]", $pCountLF), 1, "0x" _
            & "8B4C2404" _     ;~     mov  ecx, dword[esp+04h]
            & "31C0"     _     ;~     xor  eax, eax
            & "8A11"     _     ;~  @@:mov  dl, byte[ecx]
            & "80FA00"   _     ;~     cmp  dl, 00h
            & "7409"     _     ;~     je   @f
            & "41"       _     ;~     inc  ecx
            & "80FA0A"   _     ;~     cmp  dl, 0Ah
            & "75F3"     _     ;~     jne  @b
            & "40"       _     ;~     inc  eax
            & "EBF0"     _     ;~     jmp  @b
            & "C20400"   _     ;~  @@:ret  4
    )

    Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $g_sString)[0]

    _MemVirtualFree($pCountLF, 0, $MEM_RELEASE)

    Return $nResult
EndFunc

 

ha, assembly version!

how must this be used?

my test only returns 0

#include <Memory.au3>

Local $sString
For $i = 1 To 100
    $sString &= "This line is line " & $i & @CRLF
Next

ConsoleWrite(StringCountLF($sString)&@CRLF)

Func StringCountLF($sString)

    Local $g_sString ; <-- added to avoid following error
    ; "D:\Autoit\Test\1.au3" (28) : ==> Variable used without being declared.:
    ; Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $g_sString)[0]
    ; Local $nResult = DllCallAddress("INT", $pCountLF, "STR", ^ ERROR

    Local $pCountLF = _MemVirtualAlloc(Null, 25, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)

    DllStructSetData(DllStructCreate("BYTE[25]", $pCountLF), 1, "0x" _
            & "8B4C2404" _     ;~     mov  ecx, dword[esp+04h]
            & "31C0"     _     ;~     xor  eax, eax
            & "8A11"     _     ;~  @@:mov  dl, byte[ecx]
            & "80FA00"   _     ;~     cmp  dl, 00h
            & "7409"     _     ;~     je   @f
            & "41"       _     ;~     inc  ecx
            & "80FA0A"   _     ;~     cmp  dl, 0Ah
            & "75F3"     _     ;~     jne  @b
            & "40"       _     ;~     inc  eax
            & "EBF0"     _     ;~     jmp  @b
            & "C20400"   _     ;~  @@:ret  4
    )
    Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $g_sString)[0]

    _MemVirtualFree($pCountLF, 0, $MEM_RELEASE)

    Return $nResult
EndFunc

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

The error is caused by a typo, it should be like this

Func StringCountLF($g_sString)

Share this post


Link to post
Share on other sites

 

The error is caused by a typo, it should be like this

Func StringCountLF($g_sString)

 

ha yes, you are right, now it works, thanks mikell

here a new race with the new competitor

p.s.

(chimp has retired from competition. Disqualified! : )

#include <array.au3>
#include <Memory.au3>

Local $sString, $hTimer, $aCompetitors[7][2] = [ _
        ["Melba23   ", 0], _
        ["Mikell    ", 0], _
        ["Mikell 2  ", 0], _
        ["SadBunny  ", 0], _
        ["Malkey    ", 0], _
        ["jchd      ", 0], _
        ["Tekk      ", 0]]

For $i = 1 To 100000
    $sString &= "This line is line " & $i & @CRLF
Next
$hTimer = TimerInit()

StringReplace($sString, @LF, "")
$a = @extended
Stopwatch(0, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[0][0]) & @TAB & $aCompetitors[0][1] & " secs" & @TAB & $a & @CRLF)

$a = StringSplit($sString, @LF)[0] - 1
Stopwatch(1, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[1][0]) & @TAB & $aCompetitors[1][1] & " secs" & @TAB & $a & @CRLF)

$a = UBound(StringRegExp($sString, @LF, 3))
Stopwatch(2, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[2][0]) & @TAB & $aCompetitors[2][1] & " secs" & @TAB & $a & @CRLF)

$a = StringLen(StringRegExpReplace($sString, "[^\n]", ""))
Stopwatch(3, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[3][0]) & @TAB & $aCompetitors[3][1] & " secs" & @TAB & $a & @CRLF)

$a = StringLen(StringAddCR($sString)) - StringLen($sString)
Stopwatch(4, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[4][0]) & @TAB & $aCompetitors[4][1] & " secs" & @TAB & $a & @CRLF)

StringRegExpReplace($sString, "\n", "\n")
$a = @extended
Stopwatch(5, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[5][0]) & @TAB & $aCompetitors[5][1] & " secs" & @TAB & $a & @CRLF)

$a = StringCountLF($sString)
Stopwatch(6, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[6][0]) & @TAB & $aCompetitors[6][1] & " secs" & @TAB & $a & @CRLF)


_ArraySort($aCompetitors, 0, 0, 0, 1)
_ArrayDisplay($aCompetitors, "Results")

Func Stopwatch($iCompetitor, ByRef $hTimer, ByRef $aCompetitors)
    $aCompetitors[$iCompetitor][1] = Round(TimerDiff($hTimer) / 1000, 5)
    $hTimer = TimerInit()
EndFunc   ;==>Stopwatch

Func StringCountLF($g_sString)
    Local $pCountLF = _MemVirtualAlloc(Null, 25, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)
    DllStructSetData(DllStructCreate("BYTE[25]", $pCountLF), 1, "0x" _
            & "8B4C2404" _     ;~     mov  ecx, dword[esp+04h]
            & "31C0"     _     ;~     xor  eax, eax
            & "8A11"     _     ;~  @@:mov  dl, byte[ecx]
            & "80FA00"   _     ;~     cmp  dl, 00h
            & "7409"     _     ;~     je   @f
            & "41"       _     ;~     inc  ecx
            & "80FA0A"   _     ;~     cmp  dl, 0Ah
            & "75F3"     _     ;~     jne  @b
            & "40"       _     ;~     inc  eax
            & "EBF0"     _     ;~     jmp  @b
            & "C20400"   _     ;~  @@:ret  4
    )
    Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $g_sString)[0]
    _MemVirtualFree($pCountLF, 0, $MEM_RELEASE)
    Return $nResult
EndFunc
1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Hi All,

Thanks for the reply. I am sorry, i can't access to internet in last few days. I will sure inform you about which method suits for me. 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

I shortened the ASM code by 3 bytes (5 bytes are possible be replacing add with inc but add is faster or even more is possible):

 

#include <array.au3>
#include <Memory.au3>

Local $sString, $hTimer, $aCompetitors[7][2] = [ _
        ["Melba23   ", 0], _
        ["Mikell    ", 0], _
        ["Mikell 2  ", 0], _
        ["Malkey    ", 0], _
        ["jchd      ", 0], _
        ["Tekk      ", 0], _
        ["UEZ       ", 0]]

Local $iLines = 1000000
For $i = 1 To $iLines
    $sString &= "This line is line " & $i & @CRLF
Next
ConsoleWrite($iLines & " lines have been created." & @CRLF)

$hTimer = TimerInit()

StringReplace($sString, @LF, "")
$a = @extended
Stopwatch(0, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[0][0]) & @TAB & $aCompetitors[0][1] & " secs" & @TAB & $a & @CRLF)

$a = StringSplit($sString, @LF)[0] - 1
Stopwatch(1, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[1][0]) & @TAB & $aCompetitors[1][1] & " secs" & @TAB & $a & @CRLF)

$a = UBound(StringRegExp($sString, @LF, 3))
Stopwatch(2, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[2][0]) & @TAB & $aCompetitors[2][1] & " secs" & @TAB & $a & @CRLF)

$a = StringLen(StringAddCR($sString)) - StringLen($sString)
Stopwatch(3, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[3][0]) & @TAB & $aCompetitors[3][1] & " secs" & @TAB & $a & @CRLF)

StringRegExpReplace($sString, "\n", "\n")
$a = @extended
Stopwatch(4, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[4][0]) & @TAB & $aCompetitors[4][1] & " secs" & @TAB & $a & @CRLF)

$a = StringCountLF($sString)
Stopwatch(5, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[5][0]) & @TAB & $aCompetitors[5][1] & " secs" & @TAB & $a & @CRLF)

$a = StringCountLF2($sString)
Stopwatch(6, $hTimer, $aCompetitors)
ConsoleWrite(StringFormat("%-20s", $aCompetitors[6][0]) & @TAB & $aCompetitors[6][1] & " secs" & @TAB & $a & @CRLF)

_ArraySort($aCompetitors, 0, 0, 0, 1)
_ArrayDisplay($aCompetitors, "Results")

Func Stopwatch($iCompetitor, ByRef $hTimer, ByRef $aCompetitors)
    $aCompetitors[$iCompetitor][1] = Round(TimerDiff($hTimer) / 1000, 5)
    $hTimer = TimerInit()
EndFunc   ;==>Stopwatch

Func StringCountLF($g_sString)
    Local $pCountLF = _MemVirtualAlloc(Null, 25, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)
    DllStructSetData(DllStructCreate("BYTE[25]", $pCountLF), 1, "0x" _
            & "8B4C2404" _     ;~     mov  ecx, dword[esp+04h]
            & "31C0"     _     ;~     xor  eax, eax
            & "8A11"     _     ;~  @@:mov  dl, byte[ecx]
            & "80FA00"   _     ;~     cmp  dl, 00h
            & "7409"     _     ;~     je   @f
            & "41"       _     ;~     inc  ecx
            & "80FA0A"   _     ;~     cmp  dl, 0Ah
            & "75F3"     _     ;~     jne  @b
            & "40"       _     ;~     inc  eax
            & "EBF0"     _     ;~     jmp  @b
            & "C20400"   _     ;~  @@:ret  4
    )
    Local $nResult = DllCallAddress("INT", $pCountLF, "STR", $g_sString)[0]
    _MemVirtualFree($pCountLF, 0, $MEM_RELEASE)
    Return $nResult
EndFunc

Func StringCountLF2($g_sString)
    Local $tCodeBuffer = DllStructCreate("byte ASM[22]")
    $tCodeBuffer.ASM = "0x" & _
                       "8B742404" & _ ;mov esi, dword[esp+4] -> get start address (pointer) of the string
                       "31DB" & _ ;xor ebx, ebx -> set ebx = 0
                       "AC" & _ ;lodsb ebx -> load char from [esi] to al register as byte and increment esi by one
                       "3C00" & _ ;cmp al, 0 -> is char = 0
                       "7409" & _ ;jz _end -> if yes then jump to xchange and exit
                       "3C0A" & _ ;cmp al, 0Ah -> if not compare it with @lf char
                       "75F7" & _ ;jne lodsb -> if not @lf load next byte from string
                       "83C301" & _ ;add ebx, 1 -> if yes increase ebx by one -> ebx += 1 (add is faster then inc)
                       "EBF2" & _ ;jmp lodsb -> load next byte from string
                       "93" & _ ;xchg eax, ebx -> result is in ebx but return value is always eax -> swap eax with ebx
                       "C3" ;return eax
    Return DllCall("user32.dll", "uint", "CallWindowProcW", "ptr", DllStructGetPtr($tCodeBuffer), "str", $g_sString, "int", 0, "int", 0, "int", 0)[0]
EndFunc
 

1.000.000 lines will be generated.

 

 

PS: I'm learning ASM!

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Threads like this why I love to read the forums even without specific questions or anything :D

1 person likes this

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I shortened the ASM code by 3 bytes (5 bytes are possible be replacing add with inc but add is faster or even more is possible):

 

.....

 

 

PS: I'm learning ASM!

 

 

... I get following result, it seems your asm has some issue it returns too quickly and returns 0

Melba23                 0.60178 secs   75000

Mikell                  0.17548 secs   75000

Mikell 2                0.12632 secs   75000

Malkey                  0.0716 secs    75000

jchd                    0.08136 secs   75000

Tekk                    0.0305 secs    75000

UEZ                     0.0002 secs    0

p.s.

shorten to only 75000 lines for a quick test

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

What CPUs are you using?

My results:

 

Melba23                 20.77734 secs   7500000
Mikell                  8.47381 secs    7500000
Mikell 2                6.52575 secs    7500000
Malkey                  3.21329 secs    7500000
jchd                    3.67057 secs    7500000
Tekk                    1.37152 secs    7500000
UEZ                     1.34916 secs    7500000

Melba23                 2.68555 secs    1000000
Mikell                  1.09079 secs    1000000
Mikell 2                0.85604 secs    1000000
Malkey                  0.40858 secs    1000000
jchd                    0.46289 secs    1000000
Tekk                    0.17010 secs    1000000
UEZ                     0.16663 secs    1000000
My CPU:

Intel® Core i5-4300U CPU @ 1.90GHz

Instructions sets MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, EM64T, VT-x, AES, AVX, AVX2, FMA3

The result for both ASM code should be more or less the same!

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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

  • Similar Content

    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question related to strings items in an Array and sorting. Maybe someone can advice me how to solve the issue.
      I have an Array of strings, every item of the Array is as following:
      INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]  INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x I want to sort the item in the array by date and time, is there any function which allows me to sort by date/time?
      Thanks in advance
    • Innominate
      By Innominate
      How can I filter out everything in a string except for text between specific tags, specifically between <title>Message title</title> and <body id="msgFeedSummaryBody" selected="false">Summary text</body>
       
      I also want to extract the URL, which is given three times:
           1. Message-Id: <https://www.linktopage.htm@localhost.localdomain>
           2. Content-Base: https://linktopage.htm (no closing tag for this, just a line feed with the next one beginning with “Content-Type: “)
           3. <base href="https://www.linktopage.htm">
       
      Having extracted the Message title, Summary text, and URL, I want to save that in one of various text files, with a prompt for user input giving me a way (such as entering a letter, word, or clicking a radio button; the latter is preferable) to specify which file.
       
      Here is the code I have so far:
       
      #include <MsgBoxConstants.au3> Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase HotKeySet("^h", "_copy") While 1 Sleep(100) WEnd Func _copy() Send("^u") ; opens the message source of the news feed in my e-mail client (Thunderbird) showing all of the desired information Sleep(1000) Send("^a") ; select all of it Sleep(1000) Send("^c") ; copy it to clipboard EndFunc Now how can I filter the clipboard contents (as above), add to it (adding “-----------------------” as a separator, display it to the screen (for confirmation), and append it as plain text to one of several different files?
       
      Thank you!
    • vikashbitm2010
      By vikashbitm2010
      Hi all,
      I want to return string from autoit script to the python code.
      Autoit:
      Exit("22 FE 45") -> not working
      Exit(1) -> Working
      Exit(0) -> Working
      python:
      import os
      res=os.system(autoitmine.exe)
      print "res",res
      I got some hint of @ExitCode macro, but dont know how to use it to get the string in python variable 'res'.
      Please resolve this issue for me if anyone knows about this.