Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

eq compared to ==

eq compared to ==

eq compared to ==

Hi, I have read that using == to compare numbers rather than eq is more efficient and uses less CPU cycles and the same that eq should be used to compare text rather than ==. I have seen in an example code from a coder to overcome this issue, rather than use == to compare the numbers, the coder has placed the number in quotes and kept eq in use for the comparison, an example is as follows:

This was what was in the first instance:
if {($responseCode eq 403)}

This is what it was changed to so that it could overcome the issue highlighted:
if {($responseCode eq "403")}

In my opinion it should have been changed to the following so that now it would be efficient in less CPU cycles in the sense of how it has been referenced in my statement above.
if {($responseCode == 403)}

But does what the coder has done by placing the number in quotes actually done it correctly so that using the eq is now valid in the sense how eq should be used as referenced above? Or has it made no difference or made it worse in terms of CPU use?

RE: eq compared to ==

But be aware, not only speed is important, but correctness:
With numeric compare 403 is equal 403.0, but witth string compare it isn't equal.

try this:


set a 403

puts "String compare:"
if {$a eq 403 }   then  {puts "true"} else {puts "false"}
if {$a eq 403.0 } then  {puts "true"} else {puts "false"}

puts "Number compare:"
if {$a == 403}    then {puts "true"} else {puts "false"}
if {$a == 403.0 } then {puts "true"} else {puts "false"} 



$ tclsh compare.tcl
String compare:
Number compare:

According to this web page http://wiki.tcl.tk/15322 string comparition should be 20% performanter than numeric comparition.

RE: eq compared to ==

Hi, thanks for your reply. Let me ask in this way, is there any difference in how the following two perform in correctness and speed?

If {$a eq 403}
If {$a eq "403"}

Does placing the number in quotes make a difference, if so then how? For example does if {$a eq "403"} perform in the same fashion in speed and correctness as
If {$a == 403}?

RE: eq compared to ==

IMO it should be the same, because in Tcl everything is a string - http://wiki.tcl.tk/3018
But if you want to experiment you can do some comparitions as suggested here - http://wiki.tcl.tk/15322


proc p1 {a} {if {$a eq 403}   {return 1}}
proc p2 {a} {if {$a eq "403"} {return 1}}

set a 403
puts [time {p1 $a} 1000000]
puts [time {p2 $a} 1000000]

puts ""

set a "403"
puts [time {p1 $a} 1000000]
puts [time {p2 $a} 1000000] 



0.457898 microseconds per iteration
0.460915 microseconds per iteration

0.460034 microseconds per iteration
0.460491 microseconds per iteration 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close