QB64 Phoenix Edition
Why are SINGLE variables faster than INTEGER variables ? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Why are SINGLE variables faster than INTEGER variables ? (/showthread.php?tid=3769)

Pages: 1 2


Why are SINGLE variables faster than INTEGER variables ? - Elzaimer - 06-27-2025

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



RE: Why are SINGLE variables faster than INTEGER variables ? - ahenry3068 - 06-27-2025

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 ?


RE: Why are SINGLE variables faster than INTEGER variables ? - Delsus - 06-27-2025

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.


RE: Why are SINGLE variables faster than INTEGER variables ? - Elzaimer - 06-27-2025

Thanks for your answer.

I tried "AS _integer64" and i get the same time than with  INTEGER (3x slower than SINGLE)


RE: Why are SINGLE variables faster than INTEGER variables ? - Jack - 06-27-2025

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


RE: Why are SINGLE variables faster than INTEGER variables ? - SMcNeill - 06-27-2025

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?


RE: Why are SINGLE variables faster than INTEGER variables ? - Elzaimer - 06-27-2025

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


RE: Why are SINGLE variables faster than INTEGER variables ? - Kernelpanic - 06-27-2025

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



RE: Why are SINGLE variables faster than INTEGER variables ? - SMcNeill - 06-27-2025

   

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


RE: Why are SINGLE variables faster than INTEGER variables ? - Jack - 06-27-2025

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