def cout_example(lst): # get it in cache i = lst[0] inline_tools.inline('std::cout << i << std::endl;', ['i']) t1 = time.time() for i in lst: inline_tools.inline('std::cout << i << std::endl;', ['i']) t2 = time.time()
def cout_example(lst): # get it in cache i = lst[0] inline_tools.inline("std::cout << i << std::endl;", ["i"]) t1 = time.time() for i in lst: inline_tools.inline("std::cout << i << std::endl;", ["i"]) t2 = time.time()
def __init__(self, parent, id = -1, pos=wx.wxPyDefaultPosition, size=wx.wxPyDefaultSize, **attr): wx.wxWindow.__init__(self, parent, id, pos,size) #wx.EVT_PAINT(self,self.on_paint) background = wx.wxNamedColour('white') code = """ self->SetBackgroundColour(*background); """ inline_tools.inline(code,['self','background'],compiler='msvc')
def __init__(self, parent, id=-1, pos=wx.wxPyDefaultPosition, size=wx.wxPyDefaultSize, **attr): wx.wxWindow.__init__(self, parent, id, pos,size) # wx.EVT_PAINT(self,self.on_paint) background = wx.wxNamedColour('white') code = """ self->SetBackgroundColour(*background); """ inline_tools.inline(code,['self','background'],compiler='msvc')
def DoDrawing2(self, dc): red = wxNamedColour("RED") blue = wxNamedColour("BLUE") grey_brush = wxLIGHT_GREY_BRUSH code = \ """ //#line 108 "wx_example.py" dc->BeginDrawing(); dc->SetPen(wxPen(*red,4,wxSOLID)); dc->DrawRectangle(5, 5, 50, 50); dc->SetBrush(*grey_brush); dc->SetPen(wxPen(*blue, 4,wxSOLID)); dc->DrawRectangle(15, 15, 50, 50); """ inline_tools.inline(code, ['dc', 'red', 'blue', 'grey_brush'], verbose=2) dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) te = dc.GetTextExtent("Hello World") dc.DrawText("Hello World", 60, 65) dc.SetPen(wxPen(wxNamedColour('VIOLET'), 4)) dc.DrawLine(5, 65 + te[1], 60 + te[0], 65 + te[1]) lst = [(100, 110), (150, 110), (150, 160), (100, 160)] dc.DrawLines(lst, -60) dc.SetPen(wxGREY_PEN) dc.DrawPolygon(lst, 75) dc.SetPen(wxGREEN_PEN) dc.DrawSpline(lst + [(100, 100)]) #dc.DrawBitmap(self.bmp, 200, 20, true) #dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) #dc.DrawText("a bitmap", 200, 85) font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) dc.SetFont(font) dc.SetTextForeground(wxBLACK) for a in range(0, 360, 45): dc.DrawRotatedText("Rotated text...", 300, 300, a) dc.SetPen(wxTRANSPARENT_PEN) dc.SetBrush(wxBLUE_BRUSH) dc.DrawRectangle(50, 500, 50, 50) dc.DrawRectangle(100, 500, 50, 50) dc.SetPen(wxPen(wxNamedColour('RED'))) dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) self.DrawSavedLines(dc) dc.EndDrawing()
def DoDrawing2(self, dc): red = wxNamedColour("RED"); blue = wxNamedColour("BLUE"); grey_brush = wxLIGHT_GREY_BRUSH; code = \ """ //#line 108 "wx_example.py" dc->BeginDrawing(); dc->SetPen(wxPen(*red,4,wxSOLID)); dc->DrawRectangle(5, 5, 50, 50); dc->SetBrush(*grey_brush); dc->SetPen(wxPen(*blue, 4,wxSOLID)); dc->DrawRectangle(15, 15, 50, 50); """ inline_tools.inline(code,['dc','red','blue','grey_brush'],verbose=2) dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) te = dc.GetTextExtent("Hello World") dc.DrawText("Hello World", 60, 65) dc.SetPen(wxPen(wxNamedColour('VIOLET'), 4)) dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1]) lst = [(100,110), (150,110), (150,160), (100,160)] dc.DrawLines(lst, -60) dc.SetPen(wxGREY_PEN) dc.DrawPolygon(lst, 75) dc.SetPen(wxGREEN_PEN) dc.DrawSpline(lst+[(100,100)]) #dc.DrawBitmap(self.bmp, 200, 20, true) #dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) #dc.DrawText("a bitmap", 200, 85) font = wxFont(20, wxSWISS, wxNORMAL, wxNORMAL) dc.SetFont(font) dc.SetTextForeground(wxBLACK) for a in range(0, 360, 45): dc.DrawRotatedText("Rotated text...", 300, 300, a) dc.SetPen(wxTRANSPARENT_PEN) dc.SetBrush(wxBLUE_BRUSH) dc.DrawRectangle(50,500,50,50) dc.DrawRectangle(100,500,50,50) dc.SetPen(wxPen(wxNamedColour('RED'))) dc.DrawEllipticArc(200, 500, 50, 75, 0, 90) self.DrawSavedLines(dc) dc.EndDrawing()
def c_list_map2(func, seq): """ Uses Python API more than CXX to implement a simple map-like function. It does not provide any error checking. """ assert (type(func) in [FunctionType, MethodType, type(len)]) code = """ #line 40 "functional.py" py::tuple args(1); PyObject* py_args = (PyObject*)args; py::list result(seq.len()); PyObject* py_result = (PyObject*)result; PyObject* item = NULL; PyObject* this_result = NULL; int N = seq.len(); for(int i = 0; i < N;i++) { item = PyList_GET_ITEM(py_seq,i); Py_INCREF(item); PyTuple_SetItem(py_args,0,item); this_result = PyEval_CallObject(py_func,py_args); PyList_SetItem(py_result,i,this_result); } return_val = result; """ return inline_tools.inline(code, ['func', 'seq'])
def c_list_map2(func,seq): """ Uses Python API more than CXX to implement a simple map-like function. It does not provide any error checking. """ assert(type(func) in [FunctionType,MethodType,type(len)]) code = """ #line 40 "functional.py" py::tuple args(1); PyObject* py_args = (PyObject*)args; py::list result(seq.len()); PyObject* py_result = (PyObject*)result; PyObject* item = NULL; PyObject* this_result = NULL; int N = seq.len(); for(int i = 0; i < N;i++) { item = PyList_GET_ITEM(py_seq,i); Py_INCREF(item); PyTuple_SetItem(py_args,0,item); this_result = PyEval_CallObject(py_func,py_args); PyList_SetItem(py_result,i,this_result); } return_val = result; """ return inline_tools.inline(code,['func','seq'])
def print_compare(n): print('Printing %d integers:' % n) t1 = time.time() for i in range(n): print(i, end=' ') t2 = time.time() py = (t2 - t1) # get it in cache inline_tools.inline('printf("%d",i);', ['i']) t1 = time.time() for i in range(n): inline_tools.inline('printf("%d",i);', ['i']) t2 = time.time() print(' speed in python:', py) print(' speed in c:', (t2 - t1)) print(' speed up: %3.2f' % (py / (t2 - t1)))
def print_compare(n): print "Printing %d integers:" % n t1 = time.time() for i in range(n): print i, t2 = time.time() py = t2 - t1 # get it in cache inline_tools.inline('printf("%d",i);', ["i"]) t1 = time.time() for i in range(n): inline_tools.inline('printf("%d",i);', ["i"]) t2 = time.time() print " speed in python:", py print " speed in c:", (t2 - t1) print " speed up: %3.2f" % (py / (t2 - t1))
def print_compare(n): print('Printing %d integers:' % n) t1 = time.time() for i in range(n): print(i, end=' ') t2 = time.time() py = (t2-t1) # get it in cache inline_tools.inline('printf("%d",i);',['i']) t1 = time.time() for i in range(n): inline_tools.inline('printf("%d",i);',['i']) t2 = time.time() print(' speed in python:', py) print(' speed in c:',(t2 - t1)) print(' speed up: %3.2f' % (py/(t2-t1)))
def c_multi_return(): code = """ py::tuple results(2); results[0] = 1; results[1] = "2nd"; return_val = results; """ return inline_tools.inline(code,[])
def c_sort2(adict): assert(type(adict) is dict) code = """ #line 44 "dict_sort.py" py::list keys = adict.keys(); py::list items(keys.len()); keys.sort(); int N = keys.length(); for(int i = 0; i < N;i++) { items[i] = adict[int( keys[i] )]; } return_val = items; """ return inline_tools.inline(code,['adict'],verbose=1)
def c_list_map(func,seq): """ Uses CXX C code to implement a simple map-like function. It does not provide any error checking. """ assert(type(func) in [FunctionType,MethodType,type(len)]) code = """ #line 22 "functional.py" py::tuple args(1); int N = seq.len(); py::list result(N); for(int i = 0; i < N;i++) { args[0] = seq[i]; result[i] = func.call(args); } return_val = result; """ return inline_tools.inline(code,['func','seq'])
def c_list_map(func, seq): """ Uses CXX C code to implement a simple map-like function. It does not provide any error checking. """ assert (type(func) in [FunctionType, MethodType, type(len)]) code = """ #line 22 "functional.py" py::tuple args(1); int N = seq.len(); py::list result(N); for(int i = 0; i < N;i++) { args[0] = seq[i]; result[i] = func.call(args); } return_val = result; """ return inline_tools.inline(code, ['func', 'seq'])
def c_sort(adict): assert(type(adict) is dict) code = """ #line 24 "dict_sort.py" py::list keys = adict.keys(); py::list items(keys.length()); keys.sort(); PyObject* item = NULL; int N = keys.length(); for(int i = 0; i < N;i++) { item = PyList_GetItem(keys,i); item = PyDict_GetItem(adict,item); Py_XINCREF(item); PyList_SetItem(items,i,item); } return_val = items; """ return inline_tools.inline(code,['adict'])
from __future__ import absolute_import, print_function import sys sys.path.insert(0,'..') import inline_tools support_code = """ PyObject* length(std::string a) { int l = a.length(); return PyInt_FromLong(l); } """ a = 'some string' val = inline_tools.inline("return_val = length(a);",['a'], support_code=support_code) print(val)
# It isn't a "real world" test, but is somewhat informative. # C:\home\ej\wrk\scipy\weave\examples>python py_none.py # python: 0.0199999809265 # inline: 0.160000085831 # speed up: 0.124999813736 (this is about a factor of 8 slower) import time import sys sys.path.insert(0,'..') from inline_tools import inline def py_func(): return None n = 10000 t1 = time.time() for i in range(n): py_func() t2 = time.time() py_time = t2 - t1 print('python:', py_time) inline("",[]) t1 = time.time() for i in range(n): inline("",[]) t2 = time.time() print('inline:', (t2-t1)) print('speed up:', py_time/(t2-t1)) print('or (more likely) slow down:', (t2-t1)/py_time)
# python: 0.0199999809265 # inline: 0.160000085831 # speed up: 0.124999813736 (this is about a factor of 8 slower) import time import sys sys.path.insert(0, '..') from inline_tools import inline def py_func(): return None n = 10000 t1 = time.time() for i in range(n): py_func() t2 = time.time() py_time = t2 - t1 print('python:', py_time) inline("", []) t1 = time.time() for i in range(n): inline("", []) t2 = time.time() print('inline:', (t2 - t1)) print('speed up:', py_time / (t2 - t1)) print('or (more likely) slow down:', (t2 - t1) / py_time)