Processing performance of floats vs integers?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Processing performance of floats vs integers?

Post by Mistrel »

Why is it that running this same piece of code with floats produces almost no CPU activity while using integers uses about 25%?

Code: Select all

Repeat
  n.f+1
  If n.f=100000
    Delay(1)
    n.f=0
  EndIf
ForEver

Code: Select all

Repeat
  n+1
  If n=100000
    Delay(1)
    n=0
  EndIf
ForEver
venom
User
User
Posts: 56
Joined: Fri Jul 25, 2003 1:54 pm
Location: Australia

Post by venom »

Would the FPU come up as CPU usage?
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Flawed test I think! How many times is it repeated in 1 second? I doubt it's the same!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Integers are usually a bit faster, but it depends on how the expression is translated into assembly. There is a rather heavy penalty for mixing integers and floats in the same expression, though, since it adds the extra operation of converting to the correct format.

Your test is flawed. Delay(1) waits at least 1 millisecond. But it could wait 5000 if the computer is under heavy load from higher priority processes.

You need to time it all in one go (so the timer inaccuracy gets less significant) and with the debugger off.

With this test I get:
Float: 1722
Integer: 618
Quad: 3896
Double: 3805

But this test may be a bit unfair because the expressions are so short. Floats always have to be loaded onto the FPU, manipulated and stored back to memory. But integer operations can be done directly on memory, and in this case, when i is incremented, PB does not use a CPU register for it. So it's faster. More complex integer expressions will be done in the CPU registers, so there will be an extra penalty for transferring the numbers in and out of there.

Code: Select all

#Tries = 2000

time = GetTickCount_()
For U = 0 To #Tries
  Repeat
    n.f + 1
    If n = 100000
      n = 0
      Break
    EndIf
  ForEver
Next
MessageRequester("float", Str(GetTickCount_()-time))


time = GetTickCount_()
For U = 0 To #Tries
  Repeat
    i.l + 1
    If i = 100000
      i = 0
      Break
    EndIf
  ForEver
Next
MessageRequester("integer", Str(GetTickCount_()-time))


time = GetTickCount_()
For U = 0 To #Tries
  Repeat
    q.q + 1
    If q = 100000
      q = 0
      Break
    EndIf
  ForEver
Next
MessageRequester("quad", Str(GetTickCount_()-time))
Last edited by Trond on Tue Oct 07, 2008 11:49 am, edited 1 time in total.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Post by charvista »

The double gives also high performances.

Code: Select all

time = GetTickCount_() 
For U = 0 To #Tries 
  Repeat 
    d.d + 1 
    If d = 100000 
      d = 0 
      Break 
    EndIf 
  ForEver 
Next 
MessageRequester("double", Str(GetTickCount_()-time)) 
My results were:
Float: 2844
Integer: 968
Quad: 2781
Double: 1328
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

charvista wrote:My results were:
Float: 2844
Integer: 968
Quad: 2781
Double: 1328
Strange. What cpu do you have? My floats and integers are faster, but quads and doubles are much slower.
Float: 1722
Integer: 618
Quad: 3896
Double: 3805
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

E4300:

Float: 1344
Integer: 906
Quad: 2344
Double: 1312

T9300:

Float: 906
Integer: 563
Quad: 1437
Double: 813
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Processor performance can't be evaluated on such tests. On modern processors, floating point operations are blazing fast! Personally If I have floats in my code, I don't do conversion as I did by the past to make things faster with integers. However, some things, like conditional tests (if, for, etc.) are faster in integer.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Post by charvista »

I'm using AMD Athlon 64 Processor 3200+ @2.01Ghz with 2.00GB RAM.

Having Vista Ultimate, the Windows Experience Index shows:
Processor: 4.1
Memory: 4.9
Graphics: 3.2
Gaming Graphics: 3.0
Primary hard disk: 5.8

The compiled program run again shows now:
Float: 2969
Integer: 1047
Quad: 5343
Double: 1328

Of course, the speed depends of the activities of the cpu in the background. It is clear however that the Integer and the Double do have the best performances. Quad is the slowest of all.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

djes wrote:Processor performance can't be evaluated on such tests. On modern processors, floating point operations are blazing fast!
I'm just amazed that my 5 year old processor still beats the new 64-bit monsters running at 1.5 times higher clock frequency on the integer test.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

There's a lot of things to consider. The first being using 32 bits long on a 64 bits system is just bad, caches and buses are not designed for that.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

I'm using AMD Athlon 64 Processor 3500+ @994Mhz with 2.00GB RAM using Windows XP SP3.

My results are:
Float: 1297
Integer: 609
Quad: 2531
Double: 1328

As with Trond, my older system outdid a new system such as charvista's. I did match him on the Double speed though.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Tronds code is reeeeeally slow to link here (about 20 seconds) with the debugger off. With the debugger on it compiles and links almost instantly. It is the same on two different machines i've tried (XP and Vista 64). Does anyone else get that?
Mat
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Anyone interested in putting heads together and writing a little benchmark tool? Obviously it needs to be a little more sophisiticated but I'm often looking for small non-installed benchmark tools that check cpu, memory, disk IO etc.

It strikes me that the these forums would get good feedback on what kinds of tests are meaningful and which are pointless.

Just a thought. I do mean little though, I'm not suggesting large projects and souce code management, I'm thinking < 1000 lines of code for a finished tool with people passing along a function or a comment about it.

Ok, knock me down in flames now :twisted: :lol:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I think you have a very cool idea, pdwyer. I wish I could help but it's already been demonstrated that I know nothing about writing benchmark routines. Best of luck to you and anyone else who wants to participate. :)
Post Reply