Extending & Embedding Python Using C - Pi
Written by Mike James   
Monday, 02 October 2023
Article Index
Extending & Embedding Python Using C - Pi
Using the module

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

  • Example Module

Summary

  • The main principle of the C API is that all Python objects are represented by a specific C Struct that is an extension of PyObject and as a result any Python object can be referenced by a PyObject*

  • The C API contains many of the functions that implement Python. In many cases there is a simple equivalence between Python functions and C functions.

  • Every module has an initialization function with the name PyInit_modulename.

  • The initialization function is called when the module is imported and it creates the module object and returns it.

  • The PyModuleDef struct has fields that determine how the module will be treated by the system.

  • An array of PyMethodDef structs specifies the methods/functions that the module provides.

  • The PyArg_ParseTuple function can be used to parse the tuple of arguments passed to the function.

  • A simple calculation demonstrates that converting a Python function to C can speed it up by a factor of roughly 50.

  • To avoid having to change the compiler settings, a standard example extension is used in the rest of this book.

Extending & Embedding Python Using C

By Mike James

extendPython360

Buy from Amazon.

Contents

       Preface

  1. Extending And Embedding Python
  2. A First C Module Using Linux 
  3. A First C Module Using Windows
  4. Module Basics
        Extract: A First Module
        Extract: 
    Pi 
  5. Arguments
  6. Returning Python Objects
  7. Objects And Attributes
  8. More Complex Objects – Tuples, Lists and Dicts
  9. Errors, Exceptions And Reference Counting
        Extract:
    Exceptions 
  10. Bytes And Strings
  11. Modules And Attributes
  12. New Types
  13. Advanced Types
  14. Threads And The GIL
  15. Embedding Python ***NEW!!!

<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.

Banner


Valkey's Expanding Ecosystem
10/10/2024

Valkey, heralded as a better alternative to Redis, has taken the industry by storm. Reinforcing its superiority, a rapidly growing ecosystem is being built around it.



Eclipse Foundation Launches Open Regulatory Compliance Working Group
24/09/2024

The Eclipse Foundation has announced the formation of the Open Regulatory Compliance Working Group (ORC WG), which they say will support participants across the open source community in navigating and [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 02 October 2023 )