C++Guns – RoboBlog

26.03.2012

Faster 64Bit C Code - Part1

Filed under: Allgemein — Tags: , , , — Thomas @ 21:03

Auf 64Bit Plattformen ist in der Regel ein Int 32Bit groß. Macht man damit Pointerarithmetik muss die 32Bit Zahl erst zu eine 64Bit zahl convertiert werden, bevor sie verrechnet wird. Pointer sind auf 64Bit Plattformen auch 64Bit groß.

Durch den Einsatz von size_t statt int und ptrdiff_t statt int* lässt sich das verhindern. Siehe http://www.viva64.com/en/a/0050/

Ich habe mit Valgrind mir die Anzahl der Instrktionen für die Testfunktion geben lassen und es stimmt. Es werden für dieses
Beispiel nur noch 5 statt 4 Instruktionen für ein Array Element benötigt.

Hier mein Testprogramm:

#include < iostream >
#include < stdlib.h >

void func(size_t arraySize, float array[] ) {
  for (size_t i = 0; i < arraySize / 2; i++)
  {
    float value = array[i];
    array[i] = array[arraySize - i - 1];
    array[arraySize - i - 1] = value;
  }
}

int main(int argc, char **argv) {
        unsigned int n = atoi(argv[1]);
        std::cout << n << std::endl;
        float *arr = new float[n];

        func(n, arr);

        std::cout << "ausgabe " << arr[0] << std::endl;
        delete[] arr;
        return 0;
}

g++ -O2  -Wall object.cpp
valgrind --tool=callgrind   --collect-systime=yes --callgrind-out-file=unsignedint.out ./a.out 100000000
callgrind_annotate unsignedint.out |grep func

array           Instruction count
lenght     unsigned int        size_t

1*10^8    500,000,007         400,000,007 
1*10^7     50,000,007          40,000,007
1*10^6      5,000,007           4,000,007  
1*10^5        500,007             400,007
1*10^4         50,007              40,007
1*10^3          5,007               4,007

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress