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.
NIST Finalizes ‘Lightweight Cryptography’ Standard to Protect Small Devices 20/08/2025 The problem of security on small devices is a serious one so it is a good job we have the NIST on the case. Its latest effort is to point us in the direction of a new set of cryptographic functions wh [ ... ] Microsoft Open Sources 6502 Basic 05/09/2025 Microsoft has released the source code of Basic 1.1 for the 6502 microprocessor on GitHub. The page with the code says the assembly language source code represents one of the most historically signifi [ ... ] More News
Comments
or email your comment to: comments@i-programmer.info << Prev - Next |
|||
Last Updated ( Monday, 02 October 2023 ) |
Extending & Embedding Python Using C - Pi