Strange performance behavior for 64-bit modular operation

The last three of these method calls take approx. twice as many as the first four.

The only difference is that their arguments no longer fit into an integer. But in this case? The parameter is declared as long, so it should use long for calculation anyway. Does the modulo operation use a different algorithm for numbers> maxint?

I am using amd athlon64 3200+, winxp sp3 and vs2008.

       Stopwatch sw = new Stopwatch();
       TestLong(sw, int.MaxValue - 3l);
       TestLong(sw, int.MaxValue - 2l);
       TestLong(sw, int.MaxValue - 1l);
       TestLong(sw, int.MaxValue);
       TestLong(sw, int.MaxValue + 1l);
       TestLong(sw, int.MaxValue + 2l);
       TestLong(sw, int.MaxValue + 3l);
       Console.ReadLine();

    static void TestLong(Stopwatch sw, long num)
    {
        long n = 0;
        sw.Reset();
        sw.Start();
        for (long i = 3; i < 20000000; i++)
        {
            n += num % i;
        }
        sw.Stop();
        Console.WriteLine(sw.Elapsed);            
    }

EDIT: I have now tried the same with C, and the problem does not arise here, all operations with the module are performed the same way, in release mode and debug mode with and without optimization:

#include "stdafx.h"
#include "time.h"
#include "limits.h"

static void TestLong(long long num)
{
    long long n = 0;

    clock_t t = clock();
    for (long long i = 3; i < 20000000LL*100; i++)
    {
        n += num % i;
    }

    printf("%d - %lld\n", clock()-t, n);  
}

int main()
{
    printf("%i %i %i %i\n\n", sizeof (int), sizeof(long), sizeof(long long), sizeof(void*));

    TestLong(3);
    TestLong(10);
    TestLong(131);
    TestLong(INT_MAX - 1L);
    TestLong(UINT_MAX +1LL);
    TestLong(INT_MAX + 1LL);
    TestLong(LLONG_MAX-1LL);

    getchar();
    return 0;
}

EDIT2:

. , .net, c ( , ) cpu , , .

c , "_allrem". , , 32- , .net.

, c , . , .net , .

BTW: add adc. 64-, : (

EDIT3:

c Windows 7 x64, 2010. , , ( ) 64- .

+3
2

. - , : "" , Console.ReadLine, . "release". . , , . , . .

, , C.

, , , , , "" , ; , . , , "" , , , , .

+4

?

, 64- 64- , 32- , 32- . (, , JIT, ...) , ?

+3

Source: https://habr.com/ru/post/1729373/


All Articles