Speed Test...casting Ints as objects and such...

BlitzMax Forums/BlitzMax Programming/Speed Test...casting Ints as objects and such...

SculptureOfSoul(Posted 2007) [#1]
Well, I've run into the ugly problem of ints and floats and other basic data types not being objects, yet I need to store them as an object and then retrieve them again and there are a few different ways to handle this.

Can you please compile and run the following in both debug and non-debug mode, and post your results.

(Don't look at the code, it's ugly :P)

Local numiterations:Int = 100000000
Local Obj:Object
Local wrap:intwrapper = New intwrapper
Local time:Double = MilliSecs()
wrap.intval = 55
obj = "test"

Print "~n"

For Local iter1:Int = 0 Until numiterations
test( obj )
Next
Print "Convert from object to int via cast to string then int results."
Print "Elapsed time over: " + numiterations + " iterations: " + (MilliSecs()-time)

Print "~n"

time = MilliSecs()
For Local iter3:Int = 0 Until numiterations
test3( "5" )
Next
Print "Convert from string to int results."
Print "Elapsed time over: " + numiterations + " iterations: " + (MilliSecs()-time)

Print "~n"

time = MilliSecs()
For Local iter4:Int = 0 Until numiterations
test4( wrap )
Next
Print "Get int value from wrapper object results."
Print "Elapsed time over: " + numiterations + " iterations: " + (MilliSecs()-time)

Print "~n"

time = MilliSecs()
For Local iter2:Int = 0 Until numiterations
test2( iter2 )
Next
Print "No conversion whatsoever results."
Print "Elapsed time over: " + numiterations + " iterations: " + (MilliSecs()-time)




Type intwrapper
Field intval:Int
EndType

Function test:Int( obj:Object )
Return Int(String(obj))
EndFunction


Function test2:Int( pInt:Int )
Return pInt
EndFunction

Function test3:Int( obj:String )
Return Int(obj)
EndFunction

Function test4:Int( obj:Object )
Return intwrapper(obj).intval
EndFunction



Thanks much.


SculptureOfSoul(Posted 2007) [#2]
Here are my own results from my pathetic computer at work:

debug ON:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 31276.000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 25156.000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 20088.000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 17902.000000000000





debug OFF:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 9506.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 8250.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 2641.0000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 1269.0000000000000




Azathoth(Posted 2007) [#3]
On a Core 2 Duo 6700 @2.66Ghz, 3gb ram

debug on:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 6192.0000000000000

Convert from string to int results.
Elapsed time over: 100000000 iterations: 5600.0000000000000

Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 4551.0000000000000

No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 4272.0000000000000


debug off:
Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 2293.0000000000000

Convert from string to int results.
Elapsed time over: 100000000 iterations: 1775.0000000000000

Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 782.00000000000000

No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 256.00000000000000



SculptureOfSoul(Posted 2007) [#4]
Thanks Azathoth. I'm assuming most results will look similar, at least regarding the disparity between the efficiency of the different methods.

I'm now curious to see how an AMD processor will perform.

Also, looks like it'll be worth converting the little bit I've coded to an int wrapper system.


Glenn Dodd(Posted 2007) [#5]
AMD Athlon 64 Processor 3700+, 2.21GHz, 2.00 GB Ram

DEBUG ON:
Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 10652.000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 11730.000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 10603.000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 8744.0000000000000

Process complete


DEBUG OFF:
Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 3276.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 3321.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 1252.0000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 389.00000000000000

Process complete


SculptureOfSoul(Posted 2007) [#6]
That is strange.

int(string(object)) is faster than
int(string)?

Wow. Heh, very strange indeed.


xlsior(Posted 2007) [#7]
Intel Core 2 Duo E6600 (2.4GHz), latest MinGW

Debug Off:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 2492.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 2526.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 947.00000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 300.00000000000000



Debug On:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 7236.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 7293.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 5549.0000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 4837.0000000000000



SculptureOfSoul(Posted 2007) [#8]
Well, for anyone that needs to do a lot of operations dealing with storing ints in lists or in some other area where you've got to cast an int (or any basic data type) to an object it's much MUCH faster to create a wrapper object then to cast it to a string and store it as a string (which is an object, for those unaware.)

I figured there would be a speed difference, I just didn't anticipate the discrepancy being so large.


Dreamora(Posted 2007) [#9]
Core 2 Duo E6600 @ 3Ghz, 2GB RAM

Debug on:

Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 5311.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 4995.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 4304.0000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 3800.0000000000000


Debug off:
Convert from object to int via cast to string then int results.
Elapsed time over: 100000000 iterations: 1948.0000000000000


Convert from string to int results.
Elapsed time over: 100000000 iterations: 1579.0000000000000


Get int value from wrapper object results.
Elapsed time over: 100000000 iterations: 610.00000000000000


No conversion whatsoever results.
Elapsed time over: 100000000 iterations: 233.00000000000000



ziggy(Posted 2007) [#10]
My results at my work computer: (Pentium 4 2.53Ghz with 512 Mbytes of RAM)

DEBUG OFF:
Convert from object to int via cast to string then int results.

Elapsed time over: 100000000 iterations: 2935.0000000000000




Convert from string to int results.

Elapsed time over: 100000000 iterations: 2697.0000000000000




Get int value from wrapper object results.

Elapsed time over: 100000000 iterations: 1074.0000000000000




No conversion whatsoever results.

Elapsed time over: 100000000 iterations: 405.00000000000000



DEBUG ON

Convert from object to int via cast to string then int results.

Elapsed time over: 100000000 iterations: 12890.000000000000




Convert from string to int results.

Elapsed time over: 100000000 iterations: 12177.000000000000




Get int value from wrapper object results.

Elapsed time over: 100000000 iterations: 11461.000000000000




No conversion whatsoever results.

Elapsed time over: 100000000 iterations: 10365.000000000000