mIRC Home    About    Download    Register    News    Help

Print Thread
Whats Faster? $remove or $gettok #185475 09/09/07 11:47 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
var %ex = $remove(sho.#chan,sho.)

-or-

var %ex = $gettok(sho.#chan,-1,46)


Which is faster?

Last edited by DJ_Sol; 09/09/07 11:47 PM.
Re: Whats Faster? $remove or $gettok [Re: DJ_Sol] #185476 10/09/07 12:06 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
1. try benchmarking

2. consider speed in the right circumstances

i say this a lot, but everyone always brings up speed when it's least relevant :S why would you compare the speeds of these 2 distinctly different operations? it makes much more sense to discuss suitability, and the question here should be what is most suitable.

the input (sho.#chan in this case), does it always start with 'sho.'? are you always trimming off the first 4 characters? in which case $mid(sho.#chan,5) is most suitable and quite possibly the 'quickest'. i wouldn't use $remove() at all, what if the channel contains 'sho.'?

i perhaps wouldn't even use $gettok() even if the 'sho' was variable and you always wanted the channel after the first . since $gettok() removes excess delimiters and would turn #ch...an into #ch.an. this might not be an issue for you, but if i were releasing a script and interested in the most "proper" method, these are things i'd think of


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Whats Faster? $remove or $gettok [Re: jaytea] #185477 10/09/07 12:40 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Thanks for replying.

It always begins with sho. Maybe I'll just use $mid then.

Re: Whats Faster? $remove or $gettok [Re: DJ_Sol] #185479 10/09/07 02:31 AM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
As suggested, just benchmark anytime you want to figure out which is faster. To benchmark, you need to do a lot of repetitions and use $ticks to calculate it.

Example:
Code:
alias SpeedTest {
  ;Test 1
  var %ticks = $ticks, %cnt = 1
  while (%cnt <= 20000) {
    var %ex = $remove(sho.#chan,sho.)
    inc %cnt
  }
  echo -a Using Remove: $calc($ticks - %ticks)

  ;Test 2
  var %ticks = $ticks, %cnt = 1
  while (%cnt <= 20000) {
    var %ex = $gettok(sho.#chan,-1,46)
    inc %cnt
  }
  echo -a Using Gettok: $calc($ticks - %ticks)

  ;Test 3
  var %ticks = $ticks, %cnt = 1
  while (%cnt <= 20000) {
    var %ex = $left(sho.#chan,-4)
    inc %cnt
  }
  echo -a Using Left: $calc($ticks - %ticks)

  ;Test 4
  var %ticks = $ticks, %cnt = 1
  while (%cnt <= 20000) {
    var %ex = $mid(sho.#chan,5-)
    inc %cnt
  }
  echo -a Using Mid: $calc($ticks - %ticks)
}


Note that you can divide your $calc by 1000 if you want it in seconds instead of milliseconds, but it doesn't really matter.

My own tests show (after 4 tests):
$Remove: 1062, 1031, 1031, 1015
$Gettok: 1078, 1032, 1031, 1031
$Left: 1047, 1000, 1000, 985
$Mid: 1078, 1015, 1000, 1000

Just to make it easier to see, here's an average of the 4 tests:
$Remove: 1034.75
$Gettok: 1043
$Left: 1008
$Mid: 1023.25

So, just from those 4 tests at 20,000 repetitions, you can see that $Left is the fastest on average out of the 4 methods I tested. $Mid tends to be second fastest, though it was the slowest in the first test. $Remove is third and $Gettok is slightly slower or equal to $Remove.

You can perform a similar test on ANY kind of operation you want to test speed on. It isn't hard to adjust this test to work with any other command. Just replace the var lines with whatever you are testing and increase or decrease the number of tests depending on the number of methods you are testing.


Invision Support
#Invision on irc.irchighway.net
Re: Whats Faster? $remove or $gettok [Re: Riamus2] #185495 10/09/07 05:09 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Sweet thanks! $left it is!

Re: Whats Faster? $remove or $gettok [Re: DJ_Sol] #185499 10/09/07 07:08 AM
Joined: Nov 2006
Posts: 1,559
H
Horstl Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
//echo -a thats $calc((1043 - 1008) / 20000) ms faster than gettok!
But seriously: Riamus2 depicted well how you can start your own tests in the future, and jaytea pointed out that speed is not everything to take into account.

Re: Whats Faster? $remove or $gettok [Re: DJ_Sol] #185504 10/09/07 08:36 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Actually that would be $right (try $left(sho.#chan,-4) to see what it returns).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Whats Faster? $remove or $gettok [Re: qwerty] #185505 10/09/07 10:38 AM
Joined: Sep 2007
Posts: 32
S
SplitFire Offline
Ameglian cow
Offline
Ameglian cow
S
Joined: Sep 2007
Posts: 32
not much of a difference
the milliseconds lag between each method and the others can't be noticed by you as a user and won't criticaly affect any operation you might be performing using any of the methods you mentioned


on me:*:JOIN:#: { .raw part # $crlf join # }