595
Sleep() at home (programming.dev)
submitted 7 months ago by [email protected] to c/[email protected]
all 33 comments
sorted by: hot top controversial new old
[-] [email protected] 89 points 7 months ago
[-] [email protected] 23 points 7 months ago

This is brilliant.

[-] [email protected] 83 points 7 months ago

I think some compilers will just drop that in the optimization step.

[-] [email protected] 28 points 7 months ago

Real pain in the ass when you're in embedded and your carefully placed NOPs get stripped

[-] [email protected] 5 points 7 months ago

asm("nop");

[-] [email protected] 17 points 7 months ago

Homer: "oh yeah speed ~~holes~~ sleep"

[-] [email protected] 3 points 7 months ago

Sleep holes

[-] [email protected] 71 points 7 months ago

Tell the CPU to wait for you?

Na, keep the CPU busy with useless crap till you need it.

[-] [email protected] 27 points 7 months ago

Fuck those other processes. I want to hear that fan.

[-] [email protected] 4 points 7 months ago

I paid good money for my fan, I want to know it's working!

[-] [email protected] 25 points 7 months ago

Have you considered a career in middle management

[-] [email protected] 26 points 7 months ago

On microcontrollers that might be a valid approach.

[-] [email protected] 11 points 7 months ago

I've written these cycle-perfect sleep loops before.

It gets really complicated if you want to account for time spent in interrupt handlers.

[-] [email protected] 2 points 7 months ago

Thankfully I didn't need high precision realtime. I just needed to wait a few seconds for serial comm.

[-] [email protected] 1 points 7 months ago

But then I gotta buy a space heater too...

[-] [email protected] 4 points 7 months ago

Microcontrollers run 100% of the time even while sleeping.

[-] [email protected] 4 points 7 months ago

Nah, some MCUs have low power modes.
ESP32 has 5 of them, from disabling fancy features, throttling the clock, even delegating to an ultra low power coprocessor, or just going to sleep until a pin wakes it up again. It can go from 240mA to 150uA and still process things, or sleep for only 5uA.

[-] [email protected] 3 points 7 months ago* (last edited 7 months ago)

Nah, Sleeping != Low power mode. The now obsolete ATmega328 has a low power mode.

[-] [email protected] 13 points 7 months ago

This should be the new isEven()/isOdd(). Calculate the speed of the CPU and use that to determine how long it might take to achieve a 'sleep' of a required time.

[-] [email protected] 12 points 7 months ago

I took an embedded hardware class where specifically we were required to manually calculate our sleeps or use interrupts and timers rather than using a library function to do it for us.

[-] [email protected] 11 points 7 months ago* (last edited 7 months ago)
[-] [email protected] 10 points 7 months ago* (last edited 7 months ago)

Javascript enters chat:

await new Promise(r => setTimeout(r, 2000));

Which is somehow even worse.

[-] [email protected] 4 points 7 months ago

As someone who likes to use the CPU, I don't think it's worse.

[-] [email protected] 2 points 7 months ago

I mean, it’s certainly better than pre-2015.

[-] [email protected] 5 points 7 months ago

I actually remember the teacher having us do this in high school. I tried it again a few years later and it didn't really work anymore.

[-] [email protected] 13 points 7 months ago

On my first programming lesson, we were taught that 1 second sleep was for i = 1 to 1000 😀, computers was not that fast back then...

[-] [email protected] 3 points 7 months ago

I mean maybe in an early interpreted language like BASIC… even the Intel 8086 could count to 1000 in a fraction of a second

[-] [email protected] 5 points 7 months ago

This was in 1985, on a ABC80, a Swedish computer with a 3 MHz CPU. So, in theory it would be much faster, but I assume there were many performance losses (slow basic interpretor and thing like that) so that for loop got close enough to a second for us to use.

https://en.m.wikipedia.org/wiki/ABC_80

[-] [email protected] 4 points 7 months ago

I can relate. We have breaks ate work too.

[-] [email protected] 4 points 7 months ago

You gotta measure the latency of the first loop.

[-] [email protected] 2 points 7 months ago

I just measured it, and this takes 0.17 seconds. And it's really reliable, I added another zero to that number and it was 1.7 seconds

[-] [email protected] 0 points 7 months ago
this post was submitted on 24 Jan 2024
595 points (97.4% liked)

Programmer Humor

19187 readers
1270 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS