Skip to content

Instantly share code, notes, and snippets.

@mzizzi
Created November 13, 2015 00:38
Show Gist options
  • Save mzizzi/548962302a4f7a694a56 to your computer and use it in GitHub Desktop.
Save mzizzi/548962302a4f7a694a56 to your computer and use it in GitHub Desktop.
compare get_context implementations
"""
Drop this script into ./server/linux_x11/ and run it to compare get_context
implementations!
pip install -e 'git+https://github.com/rshk/python-libxdo.git#egg=python-libxdo'
pip install python-xlib
pip install psutil
In my ubuntu machine I needed the following package...
sudo aptitude install libX11-dev
My results:
running benchmarks...
new implementation 0.000524
old implementation 0.016536
done!
"""
from server_x11 import get_context
import time
import xdo
import Xlib.display
import psutil
display = Xlib.display.Display()
x = xdo.Xdo()
def get_context_quick():
window_id = x.get_focused_window_sane()
pid = x.get_pid_window(window_id)
window = display.create_resource_object('window', window_id)
window_class = window.get_wm_class()
window_title = window.get_wm_name()
process = psutil.Process(pid)
return {
'id': window_id,
'pid': pid,
'title': window_title,
'cls_name': window_class[1] if window_class is not None else None,
'cls': window_class[0] if window_class is not None else None,
'executable': process.exe(),
'cmdline': process.cmdline()
}
def bench_function(func, count=1000):
start = time.time()
for _ in range(0, count):
func()
return (time.time() - start) / count
print 'running benchmarks...'
print 'new implementation %.6f' % bench_function(get_context_quick)
print 'old implementation %.6f' % bench_function(get_context)
print 'done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment