If I have a function:
myFunc (byval a as double) as double
Is it faster to call myFunc(1.0) vs myFunc(1) and avoid a casting to double?
Rick
A question of efficiency
Re: A question of efficiency
If the casting is relevant, speedwise, you should consider inlining the function. How many times will that function be called, roughly? If it's less than a Million times, then probably casting will not make a difference. In case of doubt, time it.
Re: A question of efficiency
Yes, the casting time is already negligible in front of the only call of the function.
Re: A question of efficiency
There is no casting in anything but a toy compiler. The compiler will do the conversion from integer literal "1" to float "1.0" while generating code.
Re: A question of efficiency
You are right for this specific case.
My answer concerned the case of passing a variable of different type.
My answer concerned the case of passing a variable of different type.
Re: A question of efficiency
OP's question was whether there is a performance penalty. Perhaps "casting" is not the correct term in C/C++ jargon, but there is a conversion (except for the trivial case of literals), and it may have an impact on speed.marcov wrote:There is no casting in anything but a toy compiler. The compiler will do the conversion from integer literal "1" to float "1.0" while generating code.
Code: Select all
sub myFunc (byval a as double)
print a
end sub
Dim as double TheDouble=123.456
Dim as integer TheInteger
TheDouble=TheDouble*123.456
TheDouble=TheDouble/456
TheInteger=TheDouble
' asm int 3 ' check yourself...
myFunc(TheDouble)
asm nop
myFunc(TheInteger) '12345678h
asm nop
Sleep
Re: A question of efficiency
I spend most of my time in SQL world and 'CAST' is frequently in my vernacular.
I have functions that are common and called by many other functions that accept byval DOUBLE values. I have determined that this class spends about 45% of its time in these common functions and was looking for ways to tweak things for improved performance.
I'll do some timings with integer values vs adding .0 to them and see what difference it makes.
Rick
I have functions that are common and called by many other functions that accept byval DOUBLE values. I have determined that this class spends about 45% of its time in these common functions and was looking for ways to tweak things for improved performance.
I'll do some timings with integer values vs adding .0 to them and see what difference it makes.
Rick
Re: A question of efficiency
Rick,rpkelly wrote:I'll do some timings with integer values vs adding .0 to them and see what difference it makes.
it won't make any difference because the compiler knows what a literal value is, and generates exactly the same code for 123 and 123.00.
Have a look at my example posted above.