[Closed] strings vs ints
integers vs strings for comparison purposes:
( --how slow is searching by strings as compared to integers?
clearListener()
local myInts = for i = 1 to 200000 collect (random 0 255)
local myStrings = copy myInts
s1 = timeStamp()
for i = 1 to myInts.count do
(if myInts[i] == 2 do undefined)
e1 = timeStamp()
s2 = timeStamp()
for i = 1 to myInts.count do
(if myInts[i] as string == "2" do undefined)
e2 = timeStamp()
print "integer comparison"
print ((e1-s1)/1000.0)
print "string comparison"
print ((e2-s2)/1000.0)
print "difference"
print (((e2-s2)-(e1-s1))/1000.0)
)
well i’m never writing another string comparison again…
in maxscript, is hashing a string to an int and then comparing int to int faster than just comparing a string to a string?
why does it surprise you?
if you want to play in the same league compare int vs float, int vs char, name vs string…
i’m suprised because i assumed that comparing a string would have a similar time as comparing an integer, because characters are integers. how does max implement string comparisons? what’s the c algorithm that’s handling that? i would guess there would be only a few more lines of code to handle strings, but i really have no idea. i’m curious.
but, for small arrays the time difference is trivial – so if the code compares one or two strings, changing the code to compare integers isn’t going to be that much of a speed boost. and makes the code slightly more complex.
actually your comparison is not correct. when you check strings the conversion int to string takes the most time.
collect chars… the char elements and int elements comparison will not be so dramatically different.
i’m not converting to chars correctly, it just adds more time to the clock.
how do you convert to char? i cannot cast “as char”. what is the other way besides “as string”?
but, here is a script that compares my incorrect way of casting chars to that of strings and ints.
then it builds a little bar chart with times.
delete $*
for j = 1 to 5 do
(
clearListener(); local stepSize = 50
local myInts = for i = 1 to 100000 collect (random 0 255)
local myStrings = copy myInts
s1 = timeStamp()
for i = 1 to myInts.count do
(if myInts[i] == 2 do undefined)
e1 = timeStamp()
s2 = timeStamp()
for i = 1 to myInts.count do
(if myInts[i] as string == "2" do undefined)
e2 = timeStamp()
s3 = timeStamp()
for i = 1 to myInts.count do
(
b=memStreamMgr.openstring (myInts[i] as string)
myStream = b.readChar()
if myStream as stringStream == "2" do undefined
)
e3 = timeStamp()
local integerHeight = (e1-s1)/100.0
local stringHeight =(e2-s2)/100.0
local charHeight = (e3-s3)/100.0
local difference1 = abs((e2-s2)-(e1-s1))/100.0
local difference2 = abs((e3-s3)-(e1-s1))/100.0
local difference3 = abs((e3-s3)-(e2-s2))/100.0
local integerBox = box width:40 height:integerHeight length:40 pos:[stepSize+0,stepSize*j,0] wirecolor:(color 255 0 0)
local stringBox = box width:40 height:stringHeight length:40 pos:[stepSize+50,stepSize*j,0] wirecolor:(color 0 255 0)
local charBox = box width:40 height:charHeight length:40 pos:[stepSize+100,stepSize*j,0] wirecolor:(color 0 0 255)
local differenceBox = box width:40 height:difference1 length:40 pos:[stepSize-100,stepSize*j,0] wirecolor:(color 255 255 255)
local difference2Box = box width:40 height:difference2 length:40 pos:[stepSize-150,stepSize*j,0] wirecolor:(color 200 200 200)
local difference3Box = box width:40 height:difference3 length:40 pos:[stepSize-200,stepSize*j,0] wirecolor:(color 125 125 125)
)
myObjs = for nodes in objects collect nodes
for h = 1 to myObjs.count do
(local heightText = text size:15 kerning:0 leading:0 pos:(myObjs[h].pos+[0,0,-10])\
text:(myObjs[h].height as string) wirecolor:myObjs[h].wirecolor; rotate heightText (eulerAngles 90 0 0))
gc()
And the result of script:
legend:
red = integer comparison
green = string comparison
blue = char comparison
white = string-int, liteGrey = char-int, grey = char-string