AutoHotkey Community

It is currently May 27th, 2012, 2:07 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: January 26th, 2010, 8:36 pm 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
Very simple but I thought I'd post it up as I couldn't find a simple example when I searched... as always I'd love to see better ways.

Code:
ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
      
      a0--
   }

   Return Reversed

}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2010, 9:37 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
http://www.autohotkey.net/~hugov/tf-lib ... verseLines

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 10:32 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
simple attempt 1

Code:
var =
(Ltrim Join`n
  this
  is
  a
  test
)

msgbox % reverse(var)

reverse(var) {
   Loop,Parse,var,`n
      n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
   return n
}


greets
dR

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 10:52 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
simple attempt II
Code:
var =
(Ltrim Join`n
   this
   is
   a
   test
)
msgbox % reverse2(var)
reverse2( a,b = "",c = "init" ) { ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}


greets
dR

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 5:29 pm 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
Just for fun... I wrote a speed tester for these... I hope I did it right though it seems to work well!

With the current sets of variables it'll run for around 5 minutes. (100 runs of each of 3 functions at 1 second per run)

Updated a bit, totals aren't working exactly right (though it does report correctly per iteration) unf. I ran out of time to play with it ;)

Code:
;reverse lines speed tests

;_AHK
#SingleInstance FORCE

;VARS
TestDuration := 1000
Runs := 100

var =   
(Ltrim Join`n
  Line 000000000000000000000000000000 000000000000000000000000000000 000000000000000000000000000000
  Line 001001001001001001001001001001 001001001001001001001001001001 001001001001001001001001001001
  Line 002002002002002002002002002002 002002002002002002002002002002 002002002002002002002002002002
  Line 003003003003003003003003003003 003003003003003003003003003003 003003003003003003003003003003
  Line 004004004004004004004004004004 004004004004004004004004004004 004004004004004004004004004004
  Line 005005005005005005005005005005 005005005005005005005005005005 005005005005005005005005005005
  Line 006006006006006006006006006006 006006006006006006006006006006 006006006006006006006006006006
  Line 007007007007007007007007007007 007007007007007007007007007007 007007007007007007007007007007
  Line 008008008008008008008008008008 008008008008008008008008008008 008008008008008008008008008008
  Line 009009009009009009009009009009 009009009009009009009009009009 009009009009009009009009009009
)


;MAIN

MsgBox, OK to begin testing or PAUSE|BREAK to abort.

FileDelete, TestResults.txt

Loop, %runs%
{
   SetTimer, TestOver, -%TestDuration%
   TestRunning := 1
   RandallF_ReverseLines := 0
   While TestRunning
   {
      RandallF_ReverseLines(var)
      RandallF_ReverseLines++
   }

   TestRunning := 1
   DerRaphael_ReverseLines_1 := 0
   SetTimer, TestOver, -%TestDuration%
   While TestRunning
   {
      DerRaphael_ReverseLines_1(var)
      DerRaphael_ReverseLines_1++
   }

   TestRunning := 1
   DerRaphael_ReverseLines_2 := 0
   SetTimer, TestOver, -%TestDuration%
   While TestRunning
   {
      DerRaphael_ReverseLines_2(var)
      DerRaphael_ReverseLines_2++
   }

   Results = RandallF: %RandallF_ReverseLines%`nDerRaphael_1: %DerRaphael_ReverseLines_1%`nDerRaphael_2: %DerRaphael_ReverseLines_2%`n

   ;Totals
   DerRaphael_1tot := DerRaphael_ReverseLines_1+DerRaphael_1tot
   DerRaphael_2tot := DerRaphael_ReverseLines_2+DerRaphael_2tot
   RandallFtot := RandallF_ReverseLines+RandallFtot
   ;MsgBox % Results

   If A_Index = 1
   {
      FileAppend, Test Results of Reverse Line Order Iterations for Duration %Duration%`n, TestResults.txt
   }
   FileAppend, %Results%, TestResults.txt
} ;loop 100

Tot = TOTALS: RandallF: %RandallF_ReverseLines%`nDerRaphael_ReverseLines_1: %DerRaphael_1tot%`nDerRaphael_ReverseLines_2: %DerRaphael_2tot%`n

FileAppend, %tot%, TestResults.txt

Run, TestResults.txt

ExitApp ;Return


;LABL
TestOver:
TestRunning := 0
Return

;FUNC
DerRaphael_ReverseLines_1(var)
{
   Loop,Parse,var,`n
   n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
      return n   
}

DerRaphael_ReverseLines_2( a,b = "",c = "init" )
{ ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}


RandallF_ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
      a0--
   }
   Return Reversed   
}

;BIND
Pause::
GuiClose:
ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 29th, 2010, 4:04 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 775
Location: Texas, USA
Can I get in on this?

Simple attempt #1

Code:
var=
(Join`n
this
 is
  a
   test
)

MsgBox % jReverse(var)
Return

jReverse(p_Data)
    {
    l_AutoTrim:=A_AutoTrim
    AutoTrim off
    VarSetCapacity(Reversed,StrLen(p_Data))
    loop parse,p_Data,`n,`r
        if StrLen(Reversed)=0
            Reversed:=A_LoopField
         else
            Reversed=%A_LoopField%`n%Reversed%
    AutoTrim %l_AutoTrim%
    return Reversed
    }


Not quite as fast as some of the other functions when reversing a small dataset but difficult to beat when reversing medium to large amounts of data.

Have fun!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 29th, 2010, 7:06 am 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
Cool to see all the ways of doing this, my brother showed me in python and it's not nearly as fun because they have an optimized reverse command. :P


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 2:32 pm 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
Benchmarks of all here demonstrated functions (including the TF_ReverseLines TF StdLib mentioned by HugoV)

    Image


Code:
; QueryPerfomanceCounter - Benchmark Skript
; Version 1.0 (w) 2008 by DerRaphael / zLib License Style Release

DllCall("QueryPerformanceFrequency", "Int64*", BenchMark[F])
; BENCHMARK START
; Äußere Runden / Outer Rounds
@OR := 5
; Innere Läufe / Inner Runs
@IR := 10

; INIT VAR - use for simplicity this script as source
Gosub, GetVariable
Gosub, BuildRef

; Start Benchmark
Loop,4
{
   Setting := A_Index
   SetBatchLines, 20
   Critical, Off
   Process, Priority,, Normal
   if (A_Index = 1) {
      CurrentSettings := "SetBatchLines, 20 / Critical, Off / Process Priority Normal"
   } else if (A_Index = 2) {
      CurrentSettings := "SetBatchLines, -1 / Critical, Off / Process Priority Normal"
      SetBatchLines, -1
      Critical, Off
      Process, Priority,, Normal
   } else if (A_Index = 3) {
      CurrentSettings := "SetBatchLines, -1 / Critical, On / Process Priority Normal"
      SetBatchLines, -1
      Critical, On
      Process, Priority,, Normal
   } else if (A_Index = 4) {
      CurrentSettings := "SetBatchLines, -1 / Critical, On / Process Priority Realtime"
      SetBatchLines, -1
      Critical, On
      Process, Priority,, R
   }
   OfficialResult .= "`n" CurrentSettings "`n"
   
   Loop, % @OR
   {
      A_Run := A_Index
      Loop, % @IR
      {
         ; Zähler der auszuführenden Tests / Count of Tests to accomplish
         A_Test := 0
         
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "randallf's original function"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := ReverseLines(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - I (loop w/ ternary)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - II (recursive sort callback)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse2(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - III (loop w/ substring)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse3(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "jBalli's jReverse I"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := jReverse(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "TF_ReverseLines() from stdLib TF 3.1 by HugoV"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := TF_ReverseLines(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "QPC Benchmark BuildIn"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := QPC_BenchmarkInternal( Var )
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
      }
      ; Zeiten mitteln / Average Timings
      Loop, % A_Test
      {
         A_Test := A_Index
         Result[%A_Test%][%A_Run%] := 0
         Loop, % @IR
            Result[%A_Test%][%A_Run%] += Result[%A_Test%][%A_Run%][%A_Index%]
         Result[%A_Test%][%A_Run%] /= @IR
      }
   }
   ; BENCHMARK END
   ; Resultate ermitteln / Calculate Results
   Loop, % A_Test
   {
      A_Test := A_Index
      Result[%A_Test%] := 0
      Loop, % A_Run
         Result[%A_Test%] += Result[%A_Test%][%A_Index%]
      Result[%A_Test%] /= @OR
      OfficialResult .= Result[%A_Test%] " sec`t(" Desc[%A_Test%] ")`n"
   }
}
SetBatchLines, 20
Critical, Off
Process, Priority,, Normal

MsgBox,64,Benchmark Results %@OR% Runs with %@IR% Rounds, %OfficialResult%
ExitApp

; TEST FUNCTIONS
; Randallf's Original Function
ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}

; dR I
reverse(var) {
   Loop,Parse,var,`n
      n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
   return n
}
; dR II
reverse2( a,b = "",c = "init" ) { ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}
; dR III
reverse3(var) {
   Loop,Parse,var,`n
      n := a_loopfield "`n" n
   return SubStr( n,1,-1 )
}
; jBally I
jReverse(p_Data)
    {
    l_AutoTrim:=A_AutoTrim
    AutoTrim off
    VarSetCapacity(Reversed,StrLen(p_Data))
    loop parse,p_Data,`n,`r
        if StrLen(Reversed)=0
            Reversed:=A_LoopField
         else
            Reversed=%A_LoopField%`n%Reversed%
    AutoTrim %l_AutoTrim%
    return Reversed
   }
; buildIn
QPC_BenchmarkInternal( Var )
{
   sort, var, F SortDummyCallBack
   return var
}

GetVariable:
var =
(Ltrim Join`n
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
   sed diam nonumy eirmod tempor invidunt ut labore et dolore
   magna aliquyam erat, sed diam voluptua. At vero eos et
   accusam et justo duo dolores et ea rebum. Stet clita kasd
   gubergren, no sea takimata sanctus est Lorem ipsum dolor
   sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing
   elitr,  sed diam nonumy eirmod tempor invidunt ut labore et
   dolore magna aliquyam erat, sed diam voluptua. At vero eos
   et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum
   dolor sit amet. Lorem ipsum dolor sit amet, consetetur
   sadipscing elitr,  sed diam nonumy eirmod tempor invidunt
   ut labore et dolore magna aliquyam erat, sed diam voluptua.
   At vero eos et accusam et justo duo dolores et ea rebum.
   Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet.
   Duis autem vel eum iriure dolor in hendrerit in vulputate
   velit esse molestie consequat, vel illum dolore eu feugiat
   nulla facilisis at vero eros et accumsan et iusto odio
   dignissim qui blandit praesent luptatum zzril delenit augue
   duis dolore te feugait nulla facilisi. Lorem ipsum dolor
   sit amet, consectetuer adipiscing elit, sed diam nonummy
   nibh euismod tincidunt ut laoreet dolore magna aliquam erat
   volutpat.
   Ut wisi enim ad minim veniam, quis nostrud exerci tation
   ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
   consequat. Duis autem vel eum iriure dolor in hendrerit in
   vulputate velit esse molestie consequat, vel illum dolore
   eu feugiat nulla facilisis at vero eros et accumsan et iusto
   odio dignissim qui blandit praesent luptatum zzril delenit
   augue duis dolore te feugait nulla facilisi.
   Nam liber tempor cum soluta nobis eleifend option congue
   nihil imperdiet doming id quod mazim placerat facer possim
   assum. Lorem ipsum dolor sit amet, consectetuer adipiscing
   elit, sed diam nonummy nibh euismod tincidunt ut laoreet
   dolore magna aliquam erat volutpat. Ut wisi enim ad minim
   veniam, quis nostrud exerci tation ullamcorper suscipit
   lobortis nisl ut aliquip ex ea commodo consequat.
   Duis autem vel eum iriure dolor in hendrerit in vulputate
   velit esse molestie consequat, vel illum dolore eu feugiat
   nulla facilisis.
   At vero eos et accusam et justo duo dolores et ea rebum.
   Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus
   est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  At accusam aliquyam diam diam
   dolore dolores duo eirmod eos erat, et nonumy sed tempor et
   et invidunt justo labore Stet clita ea et gubergren, kasd
   magna no rebum. sanctus sea sed takimata ut vero voluptua.
   est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat.
   Consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus
   est Lorem ipsum dolor sit amet.
)
Return

BuildRef:
   Ref := var
   Sort,Ref,F SortDummyCallBack
Return

SortDummyCallBack(a,b,c)
{
   return c
}


The Script takes each function and benchmarks it using QPC for more accuracy. the results depend on the cpu used (in my case intel atom n270) so if you're executnig the script it may look different. but the shown tendencies should stay the same.

greets
dR

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 2:42 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
As the TF lib isn't build for speed I think you'll find that TF_ReverseLines will become very slow for large(r) files. On the otherhand you can reverse part of a file / variable which can be useful at times (e.g. reverse only the first 10 lines or so or lines 10 to 30). But nice to see the differences...

Edit: I wonder if there would be a speed difference for randalfs version if you change this Reversed := Reversed . "`n" . a%a0%
to
Code:
Reversed .= "`n" . a%a0%

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 31st, 2010, 7:23 pm 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
@HugoV:

the source is there, why dont you change the proper lines and add it as a new benchmark test and have it benched ?

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2010, 4:22 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Good idea, so .= does seem faster, even an 'older method' using Reversed = %Reversed%`n%a% is somewhat better ?

Code:
; R Mod 1
ReverseLines2(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed .= "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}

; R Mod 2
ReverseLines3(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
a:=a%a0%
Reversed = %Reversed%`n%a%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}


the results are
Quote:
SetBatchLines, 20 / Critical, Off / Process Priority Normal
0.168850 sec (randallf's original function)
0.168060 sec (R Mod 1 -> .= )
0.223324 sec (R Mod 2 -> %var% )
0.003350 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, Off / Process Priority Normal
0.000402 sec (randallf's original function)
0.000294 sec (R Mod 1 -> .= )
0.000316 sec (R Mod 2 -> %var% )
0.000420 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, On / Process Priority Normal
0.000409 sec (randallf's original function)
0.000295 sec (R Mod 1 -> .= )
0.000331 sec (R Mod 2 -> %var% )
0.000418 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, On / Process Priority Realtime
0.000427 sec (randallf's original function)
0.000287 sec (R Mod 1 -> .= )
0.000320 sec (R Mod 2 -> %var% )
0.000416 sec (QPC Benchmark BuildIn)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2010, 7:34 pm 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
Awesome stuff guys! Really cool to see the benchmarking, just what I was hoping for.

Let's switch gears for a new challenge: turn column data into tab delimited rows: (I am sure others can crush my example here)

Interesting to me at least is that if I double the column size it seems to exactly cut in half the loops.

Code:
Column1 =
(Ltrim Join`n
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
)
   
Column2 =
(Ltrim Join`n
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
)

Column3 =
(Ltrim Join`n
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
)

SetTimer, Stop, -1000


t := 1
Loops := 0

While t
{
   GoSub, ParseColumns
   Result =
   Loops++
}

msgbox %loops%
;msgbox %result%

ExitApp

Return

Stop:
t := 0
Return

ParseColumns:

   Result =

   Row := 0

   Loop, Parse, Column1, `n
   {
      Rows++ ;count the total number of rows
   }
   
   ;parse the columns and concatenate them into rows
   Loop, %rows% ;for rows
   {   
      Loop, Parse, Column%A_Index%, `n ;parse each line from the current column
      {
         Row%A_index% := Row%A_index% . A_tab . A_loopfield ;append each column entry
         StringReplace, Row%A_index%, Row%A_index%, `n ;remove the line feed
      }
      StringReplace, Row%A_index%, Row%A_index%, %A_Tab% ;remove tab from front end
   }

   Loop, %rows% ;for rows
   {
      If row%A_index% contains / ;only process and append rows that have actual data (a stamp)
      {
         Line = % Row%A_Index%
         Result := Result . Line . "`n"
      }
   }



Return


Pause::
exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 12:01 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
probly its just me, but does this work as intended?

eg is this the result you meant it to be.

Code:
gosub, getvars

o := ""
Loop,3
   stringsplit, c%A_index%n, column%A_index%, `n
loop,% c1n0
   o .= c1n%A_Index% "`t" c2n%A_Index% "`t" c3n%A_Index% "`n"
o := SubStr( o, 1, -1 )

msgbox % o

Return

getVars:

Column1 =
(Ltrim Join`n
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
)
   
Column2 =
(Ltrim Join`n
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
)

Column3 =
(Ltrim Join`n
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
)

Return


greets
dR

@HugoV which CPU did u run the benchmark on?

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 1:23 am 
That is exactly it, rows to columns.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2010, 6:43 pm 
Offline

Joined: January 29th, 2009, 9:50 pm
Posts: 483
Location: Belgium
my atempt:
Code:
var =
(Ltrim Join`n
  this
  is
  a
  test
)
Loop,Parse,var,`n
   lines := lines ? A_LoopField "`n" lines : A_LoopField
msgbox %lines%

no functions, just fast

_________________
Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], notsoobvious, Relayer and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group