Home > OS >  how to measure cython execute time to compare with c
how to measure cython execute time to compare with c

Time:11-10

I was trying to compare c and cython runtime and decide on which one should I choose for my project. So I tested a simple calculation with both. but I couldn't find any good answer to how to measure cython execute time to compare with c:

C :

#include <stdio.h>
#include <time.h>

int main() {
    int a[3] = {2, 3, 4};
    long long int n = 0;
//    long int c = 0;
    clock_t start, end;
    double cpu_time_used;
    start = clock();

    for(long long int i=0; i<1000000000; i  )
//        for(int j=0; j<10000; j  ) {
            n  = a[0]   a[2];
//            c  ;
//        }

    end = clock();
    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    printf("time : %f", cpu_time_used);
    printf("\nres : %lld", n);
//    printf("Hello, World!\n");
//    return 0;
}

Cython :

cpdef func():
    cdef int arr[3]
    arr[:] = [2, 3, 4]
    cdef unsigned long long a = 0, i
    for i in range(1000000000):
        a  = arr[0]   arr[2]
    return a

I want to know how to compare execute time of cython?

CodePudding user response:

Cython builds libraries that could be called from anywhere (here you have defined as cpdef, to be called from python as well as c). you can just call from your c program like you call any other lib.

Like in your case the function does not need any input, just directly call it from python using timeit and see the timing.

And your function has some problem, a is not initialized and the function really does not need to be really ran, the answer will always be loopcount * (arr[0] arr[2]) if a is initialized to zero.

it is also not very optimized. you can turn off the bounds check or wrap around to remove extra overheads which are not needed in your case.

CodePudding user response:

  1. Your C program will not give you any answer as the whole loop will be optimized out and the execution time of this block will be equal zero.

https://godbolt.org/z/bv793dGaa

  1. To profile python scripts use one of the available Python profilers - for example cProfile, or f you want to measure execution time of small snippet use timeit
  • Related