Isn’t doesn’t even need to be that complex, the following uses 1.25 cores at
100% on my machine:
package main
import "time"
func main() {
counter := 0
for {
counter++
time.Sleep(time.Microsecond)
}
}
even if you change it to 100 usecs, which is only 10,000 times a second, it
uses 33% of a core.
For comparison, a similar C program using usleep:
with a 1 usec delay, uses 50% of a core,
with 100 usecs, 5% of a core
The interesting thing is that at 1 usec delay, the idle wake-ups are only 100k
per sec, which means even the C program requires about 10 usec to perform the
context switch.
So if the OP is only using 33% that’s pretty good.
I am testing under OSX. So, it would appear that the Go scheduler could be
improved - 5% vs 33% is a pretty big performance hit.
> On Dec 19, 2018, at 3:44 PM, Ian Lance Taylor <[email protected]> wrote:
>
> On Wed, Dec 19, 2018 at 1:29 PM Tamás Király <[email protected]> wrote:
>>
>> my task is to update a value every microsecond.
>> i did an it with an infinite loop with time.Sleep like this
>>
>> https://play.golang.org/p/JiN3_5KiGOO
>>
>> this causes me about 50-60% of CPU usage on my machine but i made another
>> version with time.After:
>>
>> https://play.golang.org/p/PQHsNq261qZ
>>
>> this is 30%-40% of CPU usage. Can anyone optimize more this code so it does
>> not use a hilariously lot of CPU?
>
> Have you tried using time.Tick or time.NewTicker?
>
> Ian
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.