Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why are SINGLE variables faster than INTEGER variables ?
#1
Hello,

Excuse me for the poor level of my question, but I'm new to QB64pe.

I'm trying to familiarize myself with the performance of different variable types by creating some simple programs.

I created a program that searches for prime numbers, and I see that my program is three times faster when I don't define the variable type than when I do... Even though I did it to speed up execution!

without DIM: 0.82s
with DIM as _Unsigned Integer: 2.4s
with DIM as Integer: 2.4s

In my program, the values stored in the variables are integers and range from 0 to 14000. So, for me, I had to use INTEGER (only 2 bytes, so fast in my mind).

I just read that when you don't define the variable type, the default is SINGLE (4 bytes).

I wonder why SINGLE is 3 times faster than INTEGER even though it uses 2 times as many bytes?


Code: (Select All)

Dim premier As Single ' SINGLE faster than INTEGER !!! Why ?
Dim chiffreetudie As Single ' SINGLE faster than INTEGER !!! Why ?
Dim diviseur As Single ' SINGLE faster than INTEGER !!! Why ?

T0 = Timer

For chiffreetudie = 2 To 14000
    premier = 1
    For diviseur = 2 To chiffreetudie - 1
        ' Print "chiffre etudie = "; chiffreetudie; "Diviseur = "; diviseur; chiffreetudie / diviseur
        If chiffreetudie / diviseur = Int(chiffreetudie / diviseur) Then
            premier = 0
        End If
    Next diviseur
    If premier = 1 Then
        Print chiffreetudie;
    End If
Next chiffreetudie

Color 12: Print: Print "Duration : "; Timer - T0; " seconds."
Reply
#2
Modern 64 bit CPU's are complicated.    They all have FPU's also.

Try

Dim premier As _integer64 ' SINGLE faster than INTEGER !!! Why ?
Dim chiffreetudie As _integer64 ' SINGLE faster than INTEGER !!! Why ?
Dim diviseur As _integer64 ' SINGLE faster than INTEGER !!! Why ?
Reply
#3
I'll need to try that. I have a program with hundreds of DIM SHARED as INTEGER, and it takes around 60 seconds or more to be launched.
Reply
#4
Thanks for your answer.

I tried "AS _integer64" and i get the same time than with  INTEGER (3x slower than SINGLE)
Reply
#5
integer division is notoriously slow, if I am not mistaken, integer division promotes a 32-bit  dividend to 64-bit before division and likewise a 64-bit  dividend is promoted to 128-bit before division
[edit]
integer division could possibly be sped up significantly by using invariant integers, see https://gmplib.org/~tege/division-paper.pdf
Reply
#6
I get comparable times on this with all my variable types.  I certainly don't see a difference of three times the performance with SINGLE over LONG.

Code: (Select All)
T0 = Timer

For chiffreetudie = 2 To 14000
    premier = 1
    For diviseur = 2 To chiffreetudie - 1
        ' Print "chiffre etudie = "; chiffreetudie; "Diviseur = "; diviseur; chiffreetudie / diviseur
        If chiffreetudie / diviseur = Int(chiffreetudie / diviseur) Then
            premier = 0
        End If
    Next diviseur
    'If premier = 1 Then
    '    Print chiffreetudie;
    'End If
Next chiffreetudie

Color 12: Print: Print "Duration (SINGLE) : "; Timer - T0; " seconds."

T0 = Timer

For chiffreetudie = 2 To 14000
    premier = 1
    For diviseur = 2 To chiffreetudie - 1
        ' Print "chiffre etudie = "; chiffreetudie; "Diviseur = "; diviseur; chiffreetudie / diviseur
        If chiffreetudie / diviseur = chiffreetudie \ diviseur Then
            premier = 0
        End If
    Next diviseur
    'If premier = 1 Then
    '    Print chiffreetudie;
    'End If
Next chiffreetudie

Color 12: Print: Print "Duration (SINGLE, INT DIVISION) : "; Timer - T0; " seconds."




Dim premier As Long ' SINGLE faster than INTEGER !!! Why ?
Dim chiffreetudie As Long ' SINGLE faster than INTEGER !!! Why ?
Dim diviseur As Long ' SINGLE faster than INTEGER !!! Why ?

T0 = Timer

For chiffreetudie = 2 To 14000
    premier = 1
    For diviseur = 2 To chiffreetudie - 1
        ' Print "chiffre etudie = "; chiffreetudie; "Diviseur = "; diviseur; chiffreetudie / diviseur
        If chiffreetudie / diviseur = Int(chiffreetudie / diviseur) Then
            premier = 0
        End If
    Next diviseur
    'If premier = 1 Then
    '    Print chiffreetudie;
    'End If
Next chiffreetudie

Color 12: Print: Print "Duration (LONG) : "; Timer - T0; " seconds."

T0 = Timer

For chiffreetudie = 2 To 14000
    premier = 1
    For diviseur = 2 To chiffreetudie - 1
        ' Print "chiffre etudie = "; chiffreetudie; "Diviseur = "; diviseur; chiffreetudie / diviseur
        If chiffreetudie / diviseur = chiffreetudie \ diviseur Then
            premier = 0
        End If
    Next diviseur
    'If premier = 1 Then
    '    Print chiffreetudie;
    'End If
Next chiffreetudie

Color 12: Print: Print "Duration (LONG, INT DIVISION) : "; Timer - T0; " seconds."

Here's a quick test code though that will show a nice way to improve this and make it much quicker for you -- especially with integers.   Hint:  Swap out INT(x/y) for integer division with x \ y...

(06-27-2025, 10:56 AM)Jack Wrote: integer division is notoriously slow, if I am not mistaken, integer division promotes a 32-bit  dividend to 64-bit before division and likewise a 64-bit  dividend is promoted to 128-bit before division
[edit]
integer division could possibly be sped up significantly by using invariant integers, see https://gmplib.org/~tege/division-paper.pdf

Integer Division (the \ division) is much faster than taking the integer value of normal division, as shown above.

x \ y is faster than INT(x / y).   At least, it always has been for me on Windows with the math processors on my PCs and Laptops.  Perhaps Linux or Mac handles them differently somehow?
Reply
#7
Hello,

Thanks for your answer ! Smile

I get these results :   
   

With SINGLE variables the x\y gets much slower results than INT(x/y) but with LONG variables it gets much faster results  Huh
Reply
#8
Quote:SMcneill -  Integer Division (the \ division) is much faster than taking the integer value of normal division, as shown above.

x \ y is faster than INT(x / y).   At least, it always has been for me on Windows with the math processors on my PCs and Laptops.  Perhaps Linux or Mac handles them differently somehow?
This is always faster because only need to determine how many times the divisor is included in the dividend. This is easier for the computer than regular division.

Simple example:
Code: (Select All)

'Dividend - Devisor

Print 7 \ 3

Print 7 Mod 3

Print 7 / 3
Reply
#9
   

Got to say, I'm surprised by your results.  What type of machine are you running this on, just out of curiosity?  Is this something which deeply affects time on Linux/Mac?  Arm?  Or 32-bit machines? 

As you can see, my results don't match up with yours at all.  I'm just curious what the difference might be that's causing such a large change in times and results.

(06-27-2025, 02:26 PM)Kernelpanic Wrote:
Quote:SMcneill -  Integer Division (the \ division) is much faster than taking the integer value of normal division, as shown above.

x \ y is faster than INT(x / y).   At least, it always has been for me on Windows with the math processors on my PCs and Laptops.  Perhaps Linux or Mac handles them differently somehow?
This is always faster because only need to determine how many times the divisor is included in the dividend. This is easier for the computer than regular division.

Apparently not always, as per the times posted above yours.  SINGLE is faster without integer division, which really blows my mind!  I really wonder what type of machine/architecture gives such results.  Wink
Reply
#10
I am suspecting that more recent x86 CPU's or perhaps AMD vs Intel may have improved the integer division, my CPU is an Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz, 3600 Mhz, 8 Core(s), 16 Logical Processor(s), it's 7 years old
it's integer division is quite slow, my theory is that integer division is performed the old fashion way, shift and subtract whereas floating-point division is done via reciprocal and Newton-Raphson
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  _NEWIMAGE can't accept variables as dimensions? bobalooie 23 908 02-18-2026, 11:16 PM
Last Post: Unseen Machine
  generating a random number in the full range of that number? (Integer, Long) madscijr 2 641 05-01-2025, 09:11 PM
Last Post: madscijr
  testing a number's quare root is an integer and casting to a value? madscijr 22 3,374 01-29-2025, 11:12 PM
Last Post: Pete
  Determine a value is INTEGER TerryRitchie 17 3,257 07-27-2024, 05:03 PM
Last Post: Kernelpanic
  Is there a faster way to do this glow circle effect? Dav 11 2,067 06-16-2024, 11:51 PM
Last Post: Dav

Forum Jump:


Users browsing this thread: