[ Nedeljko @ 14.12.2008. 12:38 ] @
Hteo sam da isprobam svoju alternativnu funkciju za delenje sa 5 i napisao sledeći program:

Code:

#include <iostream>
#include <sys/timeb.h>
#include <cstdlib>

using namespace std;

timeb start_time;

void start() {
    ftime(&start_time);
}

void stop() {
    timeb now;
    
    ftime(&now);
    
    cout << (now.time - start_time.time)*1.0 + (now.millitm - start_time.millitm)/1000.0 << endl;
}

inline unsigned int f(unsigned int x) {
    unsigned int n = 858993459U;
    unsigned int n2 = 1717986918U;
    unsigned int p = 3435973837U;
    unsigned int r = p*++x;
    
    while (r >= n2)
        r -= n2;
    
    if (r >= n)
        r -= n;
    
    return --r;
}

int main() {
    unsigned int i = 0;
    
    start();
    
    do {
        int x = i;
        i = x;
        ++i;
    } while (i != 0);
    
    stop();
    i = 0;
    start();
    
    do {
        int x = i;
        i = i / 5;
        i = x;
        ++i;
    } while (i != 0);
    
    stop();
    i = 0;
    start();
    
    do {
        int x = i;
        i = f(i);
        i = x;
        ++i;
    } while (i != 0);
    
    stop();
    return EXIT_SUCCESS;
}


Izvršio sam program tri puta i sva triputa dobio da je od rei izlistana vremena drugo vreme kraće od prvog, tj. da se prva petlja "ubrza" dodavanjem naredbe i = i / 5;. Kako to objašnjavate?
[ EArthquake @ 15.12.2008. 20:09 ] @
ne znam , ja sam dobio "ocekivana" vremena ...

koji kompajler ?

ja sam koristio gcc na x86_64 linux-u
[ Nedeljko @ 16.12.2008. 14:20 ] @
Mozes li da ih napises?
[ EArthquake @ 16.12.2008. 17:12 ] @
Code:
ea@foundation ~]$ ./test 
52.621
44.019
115.716
[ea@foundation ~]$ ./test 
50.945
50.444
111.888
[ea@foundation ~]$ ./test 
47.362
49.047
112.492
[ea@foundation ~]$ ./test 
48.424
49.036
111.815
[ea@foundation ~]$



cudno , doduse , dok je trajao prvi test otvarao sam sajtove i radio nesto u pozadini ...
moguce da je slucajnost

tj da je u toku izvrsavanja jedne funkcije bilo vise prekljucavanja ...