A question of efficiency

General FreeBASIC programming questions.
Post Reply
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

A question of efficiency

Post by rpkelly »

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
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: A question of efficiency

Post by jj2007 »

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.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: A question of efficiency

Post by fxm »

Yes, the casting time is already negligible in front of the only call of the function.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: A question of efficiency

Post by marcov »

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.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: A question of efficiency

Post by fxm »

You are right for this specific case.
My answer concerned the case of passing a variable of different type.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: A question of efficiency

Post by jj2007 »

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.
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.

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
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Re: A question of efficiency

Post by rpkelly »

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
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: A question of efficiency

Post by jj2007 »

rpkelly wrote:I'll do some timings with integer values vs adding .0 to them and see what difference it makes.
Rick,
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.
Post Reply