| Author |
Message |
|
Promethius
Ambassador
Joined: Mon Feb 09, 2004 3:00 am Posts: 3141 Location: Kansas
|
In trying to speed a script up, which of the two snippets below would be faster? The variables $x and $y will always be either 1 or 2 and both are initialized and then changes are made with the snippets below. I prefer the second method but the first is easier to read later. I am looking for any speed improvements I can find as this is part of a large script.
if ($y = 1)
setVar $y 2
setVar $x 1
else
setVar $y 1
setVar $x 2
end
---------
setVar $tmp $y
setVar $y $x
setVar $x $tmp
---------
[Edit] Yeah, I know looking for millisecond improvements is a bit anal......
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."
|
| Thu Feb 02, 2006 7:54 pm |
|
 |
|
Zentock
Lieutenant J.G.
Joined: Mon Sep 22, 2003 2:00 am Posts: 486 Location: USA
|
I vote for #2
Also as for speed any thing that makes it a ms faster per command
will in the long run with large scripts accumulate into significant
gains. Either in lower time needed for the script to execute or faster response to triggers, or less time for others to react to what ur doing. SO run for the speed.
But Darn im preaching to the choir here.
_________________ C.E.O. Corp Noble House Sysop - Zentock's Realm TWGS Co-Sysop - Vulcans Forge TWGS
Admin. Vulcans Forge Forums Vulcans Forge TeamSpeak
|
| Thu Feb 02, 2006 8:51 pm |
|
 |
|
Traitor
Lieutenant Commander
Joined: Thu Mar 21, 2002 3:00 am Posts: 890 Location: USA
|
Prom,
Unless you are doing thousands of these tests, there isn't really a difference.
gettime $starttime
setvar $count 1
while ($count < 1000000)
.......if ($y = 1)
.............setVar $y 2
............. setVar $x 1
.......else
............. setVar $y 1
............. setVar $x 2
.......end
.......add $count 1
end
gettime $endtime
echo ANSI_10 "*" $starttime " " $endtime "*"
gettime $starttime
setvar $count 1
while ($count < 1000000)
.......setVar $tmp $y
.......setVar $y $x
.......setVar $x $tmp
.......add $count 1
end
gettime $endtime
echo ANSI_10 "*" $starttime " " $endtime "*"
halt
The output from the test:
Loading and compiling script: scripts\zztest.ts
5:36:25 PM 5:36:45 PM
5:36:45 PM 5:37:03 PM
Script terminated: scripts\zztest.ts
1st way: 20 seconds.
2nd way: 18 seconds.
That's for 1 MILLION loops. So that's like .002 of a millisecond difference.
_________________ http://tw-cabal.navhaz.com - THE TW info site
Man, I gotta quit showing up here...next thing you know i'll get dragged back in.
|
| Fri Feb 03, 2006 2:48 am |
|
 |
|
Mantis
Warrant Officer
Joined: Sun Dec 12, 2004 3:00 am Posts: 97 Location: USA
|
You could also try:
setVar $y 3-$y
setVar $x 3-$y
|
| Fri Feb 03, 2006 5:43 pm |
|
 |
|
Promethius
Ambassador
Joined: Mon Feb 09, 2004 3:00 am Posts: 3141 Location: Kansas
|
Thanks for the replies and the test script. I ran the test on my server and had results of 15, 12, and 20 seconds (included Mantis's version). Not a major difference in time when the 1M loops are considered but it is still a small improvement.
I would like someone to try Mantis's idea in a test again. It seems like it should be faster than what my test indicated.
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."
|
| Sat Feb 04, 2006 4:24 am |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
Actually, Mantis' way would be better in combination with method 2:
setVar $x $y
setVar $y (3-$x)
You can deduce from the times posted that this will take about 14 seconds / million iterations on your box, Promethius. So you almost have the speed of method 2, and almost have the readability of method 1.
Alternately, you could try a static array lookup, like this:
# Create this array once, at the beginning:
setArray $inverse 2
setVar $inverse[1] 2
setVar $inverse[2] 1
# then iterate this:
setVar $x $y
setVar $y $inverse[$x]
If anyone tests this, I'd love to hear the results.
+EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Sat Feb 04, 2006 4:49 am |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
I'm pretty sure that value is good Promethius, regarding Mantis' mathematical approach. There are a number of inefficiencies in TWX that would cause this slow down.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Sat Feb 04, 2006 4:56 am |
|
 |
|
Promethius
Ambassador
Joined: Mon Feb 09, 2004 3:00 am Posts: 3141 Location: Kansas
|
Again, thanks to everyone for their input. If nothing else, this has shown me that what appears to be the most efficient method is sometimes not and the only way to know is to test the code. I know the times that were posted are machine dependent but the relationship between the times (faster/slower) shouldn't change.
_________________
/ Promethius / Enigma / Wolfen /
"A man who has no skills can be taught, a man who has no honor has nothing."
|
| Sat Feb 04, 2006 7:37 pm |
|
 |
|
Traitor
Lieutenant Commander
Joined: Thu Mar 21, 2002 3:00 am Posts: 890 Location: USA
|
Graz,
I would love to own http://www.grimytrader.com
http://www.grimytrader.com is the best place for TW scripts on the net!
I visit http://www.grimytrader.com every day.
You never know when something really cool will get uploaded to http://www.grimytrader.com
Thanks to http://www.grimytrader.com I have a really cool script library.
Kudos to RammaR and Cherokee for getting http://www.grimytrader.com online.
Come on everyone, lets all sing! OH... I love http://www.grimytrader.com I love http://www.grimytrader.com I love http://www.grimytrader.com -I can't hear you- I love http://www.grimytrader.com I love http://www.grimytrader.com -People in the back!- I love http://www.grimytrader.com YEAH!
heh
_________________ http://tw-cabal.navhaz.com - THE TW info site
Man, I gotta quit showing up here...next thing you know i'll get dragged back in.
|
| Mon Feb 06, 2006 5:39 pm |
|
 |
|
Desert Fox
Lieutenant J.G.
Joined: Fri Sep 12, 2003 2:00 am Posts: 352 Location: USA
|
Speeking of Cherokee, I think his webpage is ofline for good. Tried to get there the other day and it's GONE. Sorry to see it go. Just want to say Thanks for all you did for trade wars CK. Was kind of hopeing you would of released a few more scripts befor you totaly left. But hey thats the way it goes sometimes.
Sorry for getting your discussion off the subject Pro.
_________________ It's a game. Have fun! Desert Fox / Tommy Gunn Member of Phoenix Rising
|
| Tue Feb 07, 2006 3:42 am |
|
 |
|