-1

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;
    clock_t start, end;
    double cpu_time_used;
    start = clock();

    for(long long int i=0; i<1000000000; i++)
            n += a[0] + a[2];

    end = clock();
    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    printf("time : %f", cpu_time_used);
    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?

sinamcr7
  • 21
  • 7
  • 1
    Use the [timeit](https://docs.python.org/3/library/timeit.html) module in the standard library. But this is important: you should check your actual use case (or something very similar to it), not some arbitrary slow function. There is no single answer to "is cython or C faster?". – Andrew Jaffe Nov 10 '22 at 12:00
  • Yuo need to enable optimizations to receive any meaningful results. With optimizations enables your C loop will be optimized out. This trivial test is useless – 0___________ Nov 10 '22 at 12:06
  • Does this answer your question? [Calculating elapsed time in a C program in milliseconds](https://stackoverflow.com/questions/1468596/calculating-elapsed-time-in-a-c-program-in-milliseconds) – wovano Nov 10 '22 at 12:09
  • https://stackoverflow.com/questions/1444428/time-stamp-in-the-c-programming-language – wovano Nov 10 '22 at 12:10
  • @AndrewJaffe Well, tbh C IS faster than Cython. Simply because Cython is, in the end, C with additional overhead. That does not mean that every program written in C is faster than the same program in Cython, simply because program in C may be very poorly optimised, so of course the rest of your statement is still valid. – matszwecja Nov 10 '22 at 12:11
  • so is it normal that this code in c runs in 1.6 sec and in cython using timeit runs in 0.025? – sinamcr7 Nov 10 '22 at 12:26
  • 1
    @matszwecja Well, yes, for any specified function, there is *a* C version that runs as fast as or faster than a cython version (with the same underlying compiler). But especially when dealing with details of, for example, transferring data between C and Python, it may be hard to write the code. – Andrew Jaffe Nov 10 '22 at 12:41
  • so its better than using c api or writing whole code in c and use it as library for my project? I want to use it in image processing is it good or might have issues with the libraries like what happened to me with pypy? – sinamcr7 Nov 10 '22 at 13:04

2 Answers2

1

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.

amirhm
  • 1,239
  • 9
  • 12
  • `loopcount * (arr[0] + arr[2])` I do not think so – 0___________ Nov 10 '22 at 12:04
  • considering that a is initialized, otherwise it is just random and useless operations which for sure will be optimized. – amirhm Nov 10 '22 at 12:07
  • about time when I'm using pycharm profiler it shows time lower than c time so I have doubt that its correct, also used python time library and it shows 0.0 sec even when I increase range to 10000000000 and more – sinamcr7 Nov 10 '22 at 12:11
  • @sinamcr7 how much you change the loop count does not matter compiler is just calculating and replacing with the formuula that is given. unless overflow of 64! should be same for your c code, maybe you the optimization is off , put -O2 and you will see zero. in python also should be zero what extra you saw is just the overhead of calling python function ( maybe few nano sec) – amirhm Nov 10 '22 at 12:13
0
  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
0___________
  • 60,014
  • 4
  • 34
  • 74