Reputation: 6320
Given an iteration:
LOOP:
for {
select {
case <-timeout:
t.Fatal("Timed out")
default:
if Count() == int64(num) {
break LOOP
}
time.Sleep(5 * time.Millisecond)
}
}
Count()
returns an int64
, so I need a conversion, and Count
changes, so we are checking here until Count()
returns an expected value - possibly thousands of iterations.
Does the compiler optimize this conversion?
Or is it better to convert num
, which is used before in other places as int
and not int64
, beforehand before starting the loop?
Upvotes: 1
Views: 299
Reputation: 418377
Whether it's optimized might depend on other code you didn't show, and also on compiler version / target architecture. Although I doubt when concurrency and other function calls are involved, the performance bottleneck will be an int
=> int64
conversion. Most likely you won't see any difference if you get rid of that conversion.
Also note that if the architecture you use is 64-bit, the size (and memory representation and interpretation) of int
and int64
is the same, which means the conversion does not incur any cost, it just changes the type (how it's interpreted).
Edit: Since you're using sleep anyway, getting rid of the conversion would be pointless. Use whichever makes your code more readable.
Upvotes: 4