Extending & Embedding Python Using C - Pi |
Written by Mike James | |||
Monday, 02 October 2023 | |||
Page 2 of 2
A Python program to make use of the module is very similar to the previous program: import Pi import time if __name__ == '__main__': N=10000000 t1=time.perf_counter() pi=Pi.myPi(1,N) t2=time.perf_counter() print((t2-t1)*1000) print(pi) So how much faster is the C extension than the pure Python? The first thing to say is that this extension should exhibit the maximum speed gain. The pure Python program delegates nothing to any existing C modules and the C extension interacts minimally with Python. The only time lost is initializing the module and making the single call to the function. On a middle-of-the range PC the pure Python program takes 2.5 seconds and the extension takes 50ms, a speedup of 50 times. On a Raspberry Pi 4 the pure Python program takes 3.2 seconds and the extension takes 89ms, a speedup of 35 times. Of course, the speedup you see from using an extension is unlikely to be this great as the task becomes more complex and requires more processing to interface the C and Python code, but it should still be worth the effort. In chapter but not in this extract
Summary
Extending & Embedding Python Using CBy Mike JamesBuy from Amazon. ContentsPreface
<ASIN:B0CK3X93KF> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
|||
Last Updated ( Monday, 02 October 2023 ) |