Balamurugan
Balamurugan

Reputation: 160

Tcl proc call in foreach loop - efficiency

In my Tcl code, I currently have a proc that returns a Tcl list. I'm calling this proc in foreach loop as shown

foreach a [get_objects] {
    # ...
}

How efficient is it with respect to Tcl? Is the following style better?

set my_list [get_objects]
foreach a $my_list {
    # ...
}

FYI, the list's length is pretty large.

Upvotes: 0

Views: 431

Answers (1)

Beinenson Ilya
Beinenson Ilya

Reputation: 7

I believe that the first one is slower. Tcl does not have compiler, due to this fact the program just "flows" without any smart memory allocations. As interpreted works, it cannot delete $my_list because it does not know the next code line, maybe the variable will be used. Probably, without knowing how Tcl interpreter, the first option will release the memory which is allocated for [get_objects]. Due to this action it will consume more time.

Upvotes: 1

Related Questions