Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eq compared to ==

Status
Not open for further replies.

goga900

Technical User
Oct 23, 2017
2
0
0
GB
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?
 
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:
Code:
[COLOR=#a52a2a][b]set[/b][/color] a [COLOR=#ff00ff]403[/color]

[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"String compare:"[/color]
[COLOR=#a52a2a][b]if[/b][/color] {[COLOR=#008b8b]$a[/color] eq [COLOR=#ff00ff]403[/color] }   [COLOR=#a52a2a][b]then[/b][/color]  {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"true"[/color]} [COLOR=#a52a2a][b]else[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"false"[/color]}
[COLOR=#a52a2a][b]if[/b][/color] {[COLOR=#008b8b]$a[/color] eq [COLOR=#ff00ff]403.0[/color] } [COLOR=#a52a2a][b]then[/b][/color]  {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"true"[/color]} [COLOR=#a52a2a][b]else[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"false"[/color]}


[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"Number compare:"[/color]
[COLOR=#a52a2a][b]if[/b][/color] {[COLOR=#008b8b]$a[/color] == [COLOR=#ff00ff]403[/color]}    [COLOR=#a52a2a][b]then[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"true"[/color]} [COLOR=#a52a2a][b]else[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"false"[/color]}
[COLOR=#a52a2a][b]if[/b][/color] {[COLOR=#008b8b]$a[/color] == [COLOR=#ff00ff]403.0[/color] } [COLOR=#a52a2a][b]then[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"true"[/color]} [COLOR=#a52a2a][b]else[/b][/color] {[COLOR=#a52a2a][b]puts[/b][/color] [COLOR=#ff00ff]"false"[/color]}

Output:
Code:
$ tclsh compare.tcl
String compare:
true
false
Number compare:
true
true

According to this web page string comparition should be 20% performanter than numeric comparition.
 
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}?
 
IMO it should be the same, because in Tcl everything is a string - But if you want to experiment you can do some comparitions as suggested here -
Code:
[COLOR=#804040][b]proc[/b][/color] p1 {a} {[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$a[/color] eq [COLOR=#ff00ff]403[/color]}   {[COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]1[/color]}}
[COLOR=#804040][b]proc[/b][/color] p2 {a} {[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$a[/color] eq [COLOR=#ff00ff]"403"[/color]} {[COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]1[/color]}}

[COLOR=#804040][b]set[/b][/color] a [COLOR=#ff00ff]403[/color]
[COLOR=#804040][b]puts[/b][/color] [[COLOR=#804040][b]time[/b][/color] {p1 [COLOR=#008080]$a[/color]} [COLOR=#ff00ff]1000000[/color]]
[COLOR=#804040][b]puts[/b][/color] [[COLOR=#804040][b]time[/b][/color] {p2 [COLOR=#008080]$a[/color]} [COLOR=#ff00ff]1000000[/color]]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]""[/color]

[COLOR=#804040][b]set[/b][/color] a [COLOR=#ff00ff]"403"[/color]
[COLOR=#804040][b]puts[/b][/color] [[COLOR=#804040][b]time[/b][/color] {p1 [COLOR=#008080]$a[/color]} [COLOR=#ff00ff]1000000[/color]]
[COLOR=#804040][b]puts[/b][/color] [[COLOR=#804040][b]time[/b][/color] {p2 [COLOR=#008080]$a[/color]} [COLOR=#ff00ff]1000000[/color]]

Output:
Code:
0.457898 microseconds per iteration
0.460915 microseconds per iteration

0.460034 microseconds per iteration
0.460491 microseconds per iteration
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top