* john_hug@mentor.com
| I my specific case, the amount of "stuff" being done inside the for
| loop is immense, so I was wondering if the 2nd example is less
| efficient (time wise) than the old style code. Mainly due to the
| number of calls to Tcl_NewStringObj() and
| Tcl_ListObjAppendElement().
The difference is how you use the result. The first form builds the
result as a string. If you use this later as a list, TCL converts it
to a list then, which delays the processing work to that later time.
The second form builds a list right now. If you use this as a list
later, the work is already done.
Possible improvements:
- c++ strings know their length, so pass argStr.size() instead of -1
as length argument for Tcl_NewStringObj() - saves one call to strlen().
If the list elements themselves are used as (sub-)lists:
- instead of manually building sublists using the + "{"+ "}" approach,
use real sublists:
Tcl_Obj *part = Tcl_NewStringObj("part", 4);
for () {
Tcl_Obj *sublist = Tcl_NewListObj( 1, &part );
Tcl_ListObjAppendElement(interp, sublist, Tcl_NewIntObj(partNumber));
Tcl_ListObjAppendElement( interp, listPtr, sublist);
}
Tcl_SetObjResult( interp, listPtr );
Note that I assume that the "{" "}" stuff is some manual
list-conversion left over from old-style TCL. If you really need the
"{}" in the data, you should keep the StringObj approach.
As always, error checking might be a good idea, too.
HTH
R'