There's probably a python C API which will give you the string length as well. It is probably more efficient to use that and avoid the strlen call. Also strdup may also be less than ideal here. It probably either reallocates memory as it reads the string or determines the string length up front, allocates space and then copies I would guess.