tcl - What is faster to execute - expr or if -


i looking @ possibility of optimizing execution of of existing code using ternary operators in single command containing if , else blocks.

is ternary operator approach faster traditional if / else e.g faster execute of following:

first:

expr {[info exists arr($var)]? [return $_timeouts($var)] : [puts "no key $var has been set"]} 

second:

if {[info exists arr($var)]} {      [return $_timeouts($var)] } else {     puts "no key $var has been set" } 

notice entire expr in ternary operator approach (first) contained in single {} block , hoping faster execute second approach.

thanks

you can use built-in time command test question.

i changed 'puts' different 'return' statement speed of variable exists in array directly compared speed of variable not exist in array.

variable arr  proc test1 { var } {   variable arr    expr {[info exists arr($var)] ? [return $arr($var)] : [return -1]} }  proc test2 { var } {   variable arr    if { [info exists arr($var)] } {     return $arr($var)   } else {     return -1   } }  proc init { } {   variable arr    # fill arr stuff...   {set 0} {$i < 10000} {incr i} {     set arr($i) $i   } }  init puts [time {test1 9000} 10000] puts [time {test1 15000} 10000] puts [time {test2 9000} 10000] puts [time {test2 15000} 10000] 

the results on machine:

bll-tecra:bll$ tclsh t.tcl 1.3121 microseconds per iteration 1.0267 microseconds per iteration 1.1399 microseconds per iteration 0.9029 microseconds per iteration 

so using expr bit slower. in case, more readable code win.

the speed difference pretty small. if small of difference affecting program, recommend trying code dictionary rather array , check speed differences.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -