Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why are SINGLE variables faster than INTEGER variables ?
#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


Messages In This Thread
RE: Why are SINGLE variables faster than INTEGER variables ? - by SMcNeill - 06-27-2025, 01:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  _NEWIMAGE can't accept variables as dimensions? bobalooie 23 911 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,260 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: 1 Guest(s)