Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Surprising differences in variable access speed.

Closed / Rejected1990276fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-06-10 23:10:41
I'm testing out adding a state option to the binary encode functions to make it simpler to write channels that use these functions. The -state option takes a variable name. In the C code we therefore get a bytearray object passed in via the objv array and may also get another object passed via name.
I get a 10x faster calling time using the pass-by-name.

% proc t1 {} {variable data; binary encode hex $data}
% proc t2 {} {variable data; binary encode hex -state data ""}
% set data [binary format @102400] ; string length [t1]
204800
% set data [binary format @102400] ; string length [t2]
204800
% set data [binary format @102400] ; time {t1} 10
1755.9 microseconds per iteration
% set data [binary format @102400] ; time {t2} 10
218.6 microseconds per iteration

!!!!!

The C function accesses these Tcl_Obj's using:
    data = input = Tcl_GetByteArrayFromObj(objv[objc-1], &count);
    if (stateObj) {
data = state = Tcl_GetByteArrayFromObj(stateObj, &state_count);
count += state_count;
    }
and then I move the 'data' pointer along the state data first then the input data. So the C code is identical for each calling method.

You can see the full function at http://paste.tclers.tk/973
msoferplain2008-06-12 03:49:25
Logged In: YES 
user_id=148712
Originator: NO

This is a bug in the interpretation of the timing script: [t2] will reset $data to {} after the first run, so that the remaining 9 runs are done on empty data.

Proper timing gives

% proc t1 {} {variable data; binary encode hex $data}
% proc t2 {} {variable data; binary encode hex -state data ""}
% set data0 [binary format @102400]; string length $data0
102400
% time {set data $data0; t1} 10
1032.5 microseconds per iteration
% time {set data $data0; t2}
1010 microseconds per iteration
Log in (anonymous is fine) to add a comment.