Created
November 25, 2014 19:31
-
-
Save smerritt/3f487c4869b9da2bb4c1 to your computer and use it in GitHub Desktop.
Benchmarking Python syscalls vs. exceptions vs. funcalls and such
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import benchmark | |
import ctypes | |
import ctypes.util | |
import os | |
DEVZERO = open('/dev/zero', 'r') | |
DEVZERO_FILENO = DEVZERO.fileno() | |
CTYPES_READ = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True).read | |
CTYPES_BUFFER = ctypes.create_string_buffer(64) | |
def returns_a_value(): | |
return 1 | |
def raises_ioerror(): | |
raise IOError(12, "oops") | |
class BenchmarkFlowControl(benchmark.Benchmark): | |
def a_method(self): | |
return 123 | |
def test_exception(self): | |
try: | |
raises_ioerror() | |
except IOError: | |
pass | |
def test_funcall(self): | |
if returns_a_value() == 1: | |
pass | |
else: | |
return "blah blah doesn't happen" | |
def test_method(self): | |
if self.a_method == 123: | |
pass | |
else: | |
return "blah blah doesn't happen" | |
def test_syscall_via_libc_ctypes(self): | |
CTYPES_READ(DEVZERO_FILENO, CTYPES_BUFFER, 2) | |
def test_syscall_via_python(self): | |
os.read(DEVZERO_FILENO, 1) | |
if __name__ == '__main__': | |
benchmark.main(each=100000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~$ python bench.py | |
Benchmark Report | |
================ | |
BenchmarkFlowControl | |
-------------------- | |
name | rank | runs | mean | sd | timesBaseline | |
-------------------|------|-------|-----------|-----------|-------------- | |
funcall | 1 | 1e+05 | 5.054e-06 | 1.028e-05 | 1.0 | |
method | 2 | 1e+05 | 5.496e-06 | 1.28e-05 | 1.0875915267 | |
syscall via python | 3 | 1e+05 | 6.723e-06 | 1.123e-05 | 1.33035478392 | |
syscall via libc | 4 | 1e+05 | 8.254e-06 | 9.457e-06 | 1.63330675599 | |
exception | 5 | 1e+05 | 8.858e-06 | 1.065e-05 | 1.75288261936 | |
Each of the above 500000 runs were run in random, non-consecutive order by | |
`benchmark` v0.1.5 (http://jspi.es/benchmark) with Python 2.7.3 | |
Linux-3.13.0-37-generic-x86_64 on 2014-11-03 18:46:55. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment