mirror of
https://github.com/boostorg/python.git
synced 2026-01-21 05:02:17 +00:00
See Python C++-SIG thread: "object.attr(object& attrname) proposal"
Started 2008-05-25 by hohehohe2@gmail.com. Excerpts: If char const* is passed to objecjt.attr(), it uses PyObject_GetAttrStrng() or PyObject_SetAttrStrng(). If object is passed to objecjt.attr(), it takes the object as a Python string object and uses PyObject_GetAttr() or PyObject_SetAttr(). If attr() behaves like this, it can be useful when there are lots of objects which you know have the same attribute name. You can save time by first making a boost::python::object and passing it to every object's attr() inside a loop. I just made a bit of modification to boost:python locally and did a quick test, like test 1: for(int i = 0; i < n; ++i) { omain.attr(attrname) = 444; //attrname is a char const* } test 2: for(int i = 0; i < n; ++i) { object o = omain.attr(attrname); //attrname is a char const* } test 3: for(int i = 0; i < n; ++i) { omain.attr(oaaaa) = 444; //oaaaa is boost::python::object that represents a string } test 4: for(int i = 0; i < n; ++i) { object o = omain.attr(oaaaa); //oaaaa is boost::python::object that represents a string } and it reasonably reflected the difference between PyObject_*Attr() and PyObject_*AttrString. test 1 :2783ms test 2 :2357ms test 3 :1882ms test 4 :1267ms test5: PyObject_SetAttrString(po_main, "aaaa", po_num444); test6: Py_DECREF(PyObject_GetAttrString(po_main, "aaaa")); test7: PyObject_SetAttr(po_main, po_aaaa, po_num444); test8: Py_DECREF(PyObject_GetAttr(po_main, po_aaaa)); (po_ prefixed variables are PyObject*), all inside each for loop, and the results were test 5 :2410ms test 6 :2277ms test 7 :1629ms test 8 :1094ms It's boost 1.35.0, Python 2.5 on linux(gcc4.1.2). I also did the same test on windows(vs8) and the tendency was not so different. [SVN r45918]
This commit is contained in:
55
test/object.cpp
Executable file → Normal file
55
test/object.cpp
Executable file → Normal file
@@ -38,26 +38,61 @@ object obj_getattr(object x, char const* name)
|
||||
return x.attr(name);
|
||||
}
|
||||
|
||||
object obj_objgetattr(object x, object const& name)
|
||||
{
|
||||
return x.attr(name);
|
||||
}
|
||||
|
||||
object obj_const_getattr(object const& x, char const* name)
|
||||
{
|
||||
return x.attr(name);
|
||||
}
|
||||
|
||||
object obj_const_objgetattr(object const& x, object const& name)
|
||||
{
|
||||
return x.attr(name);
|
||||
}
|
||||
|
||||
void obj_setattr(object x, char const* name, object value)
|
||||
{
|
||||
x.attr(name) = value;
|
||||
}
|
||||
|
||||
void obj_objsetattr(object x, object const& name, object value)
|
||||
{
|
||||
x.attr(name) = value;
|
||||
}
|
||||
|
||||
void obj_setattr42(object x, char const* name)
|
||||
{
|
||||
x.attr(name) = 42;
|
||||
}
|
||||
|
||||
void obj_objsetattr42(object x, object const& name)
|
||||
{
|
||||
x.attr(name) = 42;
|
||||
}
|
||||
|
||||
void obj_moveattr(object& x, char const* src, char const* dst)
|
||||
{
|
||||
x.attr(dst) = x.attr(src);
|
||||
}
|
||||
|
||||
void obj_objmoveattr(object& x, object const& src, object const& dst)
|
||||
{
|
||||
x.attr(dst) = x.attr(src);
|
||||
}
|
||||
|
||||
void obj_delattr(object x, char const* name)
|
||||
{
|
||||
x.attr(name).del();
|
||||
}
|
||||
|
||||
void obj_objdelattr(object x, object const& name)
|
||||
{
|
||||
x.attr(name).del();
|
||||
}
|
||||
|
||||
object obj_getitem(object x, object key)
|
||||
{
|
||||
return x[key];
|
||||
@@ -108,11 +143,21 @@ bool test_attr(object y, char* name)
|
||||
return y.attr(name);
|
||||
}
|
||||
|
||||
bool test_objattr(object y, object& name)
|
||||
{
|
||||
return y.attr(name);
|
||||
}
|
||||
|
||||
bool test_not_attr(object y, char* name)
|
||||
{
|
||||
return !y.attr(name);
|
||||
}
|
||||
|
||||
bool test_not_objattr(object y, object& name)
|
||||
{
|
||||
return !y.attr(name);
|
||||
}
|
||||
|
||||
bool test_item(object y, object key)
|
||||
{
|
||||
return y[key];
|
||||
@@ -301,11 +346,17 @@ BOOST_PYTHON_MODULE(object_ext)
|
||||
def("number", number);
|
||||
|
||||
def("obj_getattr", obj_getattr);
|
||||
def("obj_objgetattr", obj_objgetattr);
|
||||
def("obj_const_getattr", obj_const_getattr);
|
||||
def("obj_const_objgetattr", obj_const_objgetattr);
|
||||
def("obj_setattr", obj_setattr);
|
||||
def("obj_objsetattr", obj_objsetattr);
|
||||
def("obj_setattr42", obj_setattr42);
|
||||
def("obj_objsetattr42", obj_objsetattr42);
|
||||
def("obj_moveattr", obj_moveattr);
|
||||
|
||||
def("obj_objmoveattr", obj_objmoveattr);
|
||||
def("obj_delattr", obj_delattr);
|
||||
def("obj_objdelattr", obj_objdelattr);
|
||||
|
||||
def("obj_getitem", obj_getitem);
|
||||
def("obj_getitem3", obj_getitem);
|
||||
@@ -319,7 +370,9 @@ BOOST_PYTHON_MODULE(object_ext)
|
||||
def("test_not", test_not);
|
||||
|
||||
def("test_attr", test_attr);
|
||||
def("test_objattr", test_objattr);
|
||||
def("test_not_attr", test_not_attr);
|
||||
def("test_not_objattr", test_not_objattr);
|
||||
|
||||
def("test_item", test_item);
|
||||
def("test_not_item", test_not_item);
|
||||
|
||||
Reference in New Issue
Block a user