tungwaiyip.info

home

about me

links

my software

Media

Yucatán Photos

St Lucia Photos

Photo Album

Videos

Blog

< July 2009 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
262728293031 

past articles »

Click for San Francisco, California Forecast

San Francisco, USA

 

ctype performance benchmark

I have done some performance benchmarking for Python's ctypes library. I am planning to use ctypes as an alternative to writing C extension module for performance enhancement. Therefore my use case is slight different from the typical use case for accessing existing third party C libraries. In this case I am both the user and the implementer of the C library.

In order to determine what is the right granularity for context switching between Python and C, I have done some benchmarking. I mainly want to measure the function call overhead. So the test functions are trivial function like returning the first character of a string. I compare a pure Python function versus C module function versus ctypes function. The tests are ran under Python 2.6 on Windows XP with Intel 2.33Ghz Core Duo.

First of all I want to compare the function to get the first character of a string. The most basic case is to reference it as the 0th element of a sequence without calling any function. The produce the fastest result at 0.0659 usec per loop.

  $ timeit "'abc'[0]"

  10000000 loops, best of 3: 0.0659 usec per loop

As soon as I build a function around it, the cost goes up substantially. Both pure Python and C extension method shows similar performance at around 0.5 usec. ctypes function takes about 2.5 times as long at 1.37 usec.

  $ timeit -s "f=lambda s: s[0]"  "f('abc')"

  1000000 loops, best of 3: 0.506 usec per loop

  $ timeit -s "import mylib" "mylib.py_first('abc')"

  1000000 loops, best of 3: 0.545 usec per loop

  $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')"
              "dll.first('abc')"

  1000000 loops, best of 3: 1.37 usec per loop

I repeated the test with a long string (1MB). There are not much difference in performance. So I can be quite confident that the parameter is passed by reference (of the internal buffer).

  $ timeit -s "f=lambda s: s[0]; lstr='abcde'*200000"
              "f(lstr)"

  1000000 loops, best of 3: 0.465 usec per loop

  $ timeit -s "import mylib; lstr='abcde'*200000"
              "mylib.py_first(lstr)"

  1000000 loops, best of 3: 0.539 usec per loop

  $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')"
           -s "lstr='abcde'*200000"
              "dll.first(lstr)"

  1000000 loops, best of 3: 1.4 usec per loop

Next I have make some attempts to speed up ctypes performance. A measurable improvement can be attained by eliminating the attribute look up for the function. Curiously this shows no improvement in the similar case for C extension.

  $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd');
           -s "f=dll.first"
              "f('abcde')"

  1000000 loops, best of 3: 1.18 usec per loop

Secondary I have tried to specify the ctypes function prototype. This actually decrease the performance significantly.

  $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')"
           -s "f=dll.first"
           -s "f.argtypes=[ctypes.c_char_p]"
           -s "f.restype=ctypes.c_int"
              "f('abcde')"

  1000000 loops, best of 3: 1.57 usec per loop

Finally I have tested passing multiple parameters into the function. One of the parameter is passed by reference in order to return a value. Performance decrease as the number of parameter increase.

  $ timeit -s "charAt = lambda s, size, pos: s[pos]"
           -s "s='this is a test'"
              "charAt(s, len(s), 1)"

  1000000 loops, best of 3: 0.758 usec per loop

  $ timeit -s "import mylib; s='this is a test'"
              "mylib.py_charAt(s, len(s), 1)"

  1000000 loops, best of 3: 0.929 usec per loop

  $ timeit -s "import ctypes"
           -s "dll = ctypes.CDLL('mylib.pyd')"
           -s "s='this is a test'"
           -s "ch = ctypes.c_char()"
              "dll.charAt(s, len(s), 1, ctypes.byref(ch))"

  100000 loops, best of 3: 2.5 usec per loop

One style of coding that improve the performance somewhat is to build a C struct to hold all the parameters.

  $ timeit -s "from test_mylib import dll, charAt_param"
           -s "s='this is a test'"
           -s "obj = charAt_param(s=s, size=len(s), pos=3, ch='')"
              "dll.charAt_struct(obj)"

  1000000 loops, best of 3: 1.71 usec per loop

This may work because most of the fields in the charAt_param struct are invariant in the loop. Having them in the same struct object save them from getting rebuilt each time.

My overall observation is that ctypes function has an overhead that is 2 to 3 times to a similar C extension function. This may become a limiting factor if the function calls are fine grained. Using ctypes for performance enhancement is a lot more productive if the interface can be made to medium or coarse grained.

A snapshot of the source code used for testing is available for download. This is also useful if you want a boiler plate for building your own ctypes library.

2009.07.16 [] - comments

 

 

blog comments powered by Disqus

past articles »

 

Kontagent

Kontagent is hiring software engineers

BBC News

 

Syrian veto a 'licence to kill' (05 Feb 2012)

 

Seven dead in Kandahar car bomb (05 Feb 2012)

 

Transport hit as snow sweeps in (05 Feb 2012)

 

Mitt Romney wins Nevada caucuses (05 Feb 2012)

 

Fidel Castro launches his memoirs (04 Feb 2012)

 

Thousands in rival Moscow marches (04 Feb 2012)

 

Europe 'at risk of early grave' (04 Feb 2012)

 

William starts Falklands duties (04 Feb 2012)

 

'Wall-E'-style robot cleans tower (04 Feb 2012)

 

VIDEO: Big freeze continues across Europe (05 Feb 2012)

more »

 

Slashdot News for nerds, stuff that matters

 

New Hampshire Passes 'Open Source Bill' (2012-02-05T01:25:00+00:00)

 

Text Message Brands Quebec Man a Terror Suspect (2012-02-05T00:21:00+00:00)

 

Apple Overturns Motorola's German iPad and iPhone Sales Bans (2012-02-04T23:10:00+00:00)

 

LibreOffice Developer Community Increasingly Robust (2012-02-04T22:09:00+00:00)

 

Canada's Massive Public Traffic Surveillance System (2012-02-04T21:01:00+00:00)

 

German Government Endorses Chrome As Most Secure Browser (2012-02-04T19:54:00+00:00)

 

Milky Way Magnetic Fields Charted (2012-02-04T18:50:00+00:00)

 

New Book Helps You Start Contributing To Open Source (2012-02-04T17:45:00+00:00)

more »

 

TechPsychic Tech Rumors and Invented News

 

TechPsychic: AT&T: more money, says it's disruptive in funding from. (08 May 2010)

 

TechPsychic: I know that Apple is close to Apple Dominates, Hires ex-Googler - Yes, Android phones. (08 May 2010)

 

TechPsychic: AT&T says: Facebook Connect. (08 May 2010)

 

TechPsychic: Google's Nexus One of Google Chrome Release Adds Support subscriptions accounted for Amazon: Apple. (08 May 2010)

 

TechPsychic: Another stat: Twitter's Design of this is giving rise of BlackBerry Foursquare Map App store end. (07 May 2010)

 

TechPsychic: Like educational sales Up around Apple iPad makes money Plan costs half an Apple. (07 May 2010)

 

TechPsychic: Instead added extensions, social Networks than double, everyone jumps in Silicon Valley? (07 May 2010)

 

TechPsychic: So why iTunes App lets Social Networks Verizon Wireless Internet. (07 May 2010)

more »

 

SF Gate

 

University High's Reynolds inspires NY Giants (2012-02-04T22:16:03PST)

 

New Mid-Market businesses boost foot traffic (2012-02-04T22:16:03PST)

 

GOP candidates shy away from Nevada housing crisis (2012-02-04T22:16:03PST)

 

Nevada GOP caucuses include unusual rules (2012-02-04T22:16:03PST)

 

Rwandan teen to return home with new smile (2012-02-04T22:16:03PST)

 

Presented By: (04 Feb 2012)

 

Protests spur park service to rethink dog policies (2012-02-04T22:16:03PST)

 

10 Super Bowl storylines (2012-02-04T22:16:03PST)

 

Jobs report lifts Dow to highest mark since '08 (2012-02-04T22:02:20PST)

 

Hiring surges in January; jobless rate at 8.3 pct. (2012-02-04T22:02:20PST)

 

Presented By: (03 Feb 2012)

 

US service industry growth surges in January (2012-02-04T22:02:20PST)

 

S.F.'s Metreon ready for its 2nd act (2012-02-04T22:02:20PST)

 

SF Bay Area restaurants rebound along with economy (2012-02-04T22:02:20PST)

more »

 

Asia Times Online

 

AN ASIA TIMES ONLINE EXCLUSIVE : Taliban eat into Afghanistan's core (3 Feb 2012)

 

Rants and raves for new US pullout plan (3 Feb 2012)

 

THE ROVING EYE : Exposed: The Arab agenda in Syria (3 Feb 2012)

 

Question time for North Korea (3 Feb 2012)

 

From sex to shame, a guru's legacy (3 Feb 2012)

 

Hidden hand, clean hand in Russian politics (3 Feb 2012)

 

Nightmare at Narita (3 Feb 2012)

 

BOOK REVIEW : LeT: Terror incorporated (3 Feb 2012)

 

SPEAKING FREELY : Lest we forget in Myanmar (3 Feb 2012)

 

bn shale-gas deal sweetens Beijing trip by Canada's Harper (3 Feb 2012)

 

Factory owners mourn Mazar-e-Sharif clean-up (3 Feb 2012)

 

IT WORLD : Facebook heads for IPO (3 Feb 2012)

more »

 


Site feed Updated: 2012-Feb-05 03:00