keerthan kumar
keerthan kumar

Reputation: 393

tcl time taken when many procs are loaded

is there any performance (time taken) difference between below 2 scenarios.

  1. only one TCL proc is loaded in the shell and you are executing that proc.
  2. 1000 different procs are loaded to shell your accessing the same proc as case1.

Upvotes: 0

Views: 99

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137627

The costs of a procedure are due to:

  1. Defining it when you call proc. Usually pretty quick; it's just got to update a few hash tables and allocate a bit of memory to store the string representation of the body.
  2. Compiling it to bytecode. Not super-fast, but still not too bad as the compiler is written to be fairly rapid and Tcl's pretty aggressive about caching bytecode under the hood. Typically happens when you first run the procedure (as there's an internal call to do “get the bytecode, compiling the source if you need to first”).
  3. Entering (and leaving) the procedure's stack frame. More memory allocation and initalisation, but uses a special memory allocator to make this faster. This is also where arguments to procedures are stored into local variables for each of the the formal parameters.
  4. Actually running the bytecode. Depends on what you write in your procedures really.
  5. (Not procedure-specific.) There's also a cost to looking up a command. Tcl does some caching to try to keep this cost as low as possible, even leaving aside the bytecode compilation process.

I advise not worrying too much about efficiency of procedures in themselves (as opposed to the efficiency of the code you put in them), and instead partition your code in ways that make it as clear and easy to understand as possible. Yes, there are things you can do to greatly speed them up if you've got an identified bottleneck (such as targeted bits of C code), but the actual total number of procedures is only very very rarely an issue.

Upvotes: 2

Related Questions