this post was submitted on 24 Jan 2024
541 points (97.4% liked)

Programmer Humor

25699 readers
1234 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 2 years ago
MODERATORS
 
all 25 comments
sorted by: hot top controversial new old
[–] EisFrei@lemmy.world 83 points 2 years ago (1 children)
[–] tryptaminev@feddit.de 23 points 2 years ago

This is brilliant.

[–] Darkassassin07@lemmy.ca 63 points 2 years ago (2 children)

Tell the CPU to wait for you?

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

[–] jaybone@lemmy.world 25 points 2 years ago (1 children)

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

[–] leclownfou@sh.itjust.works 1 points 2 years ago

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

[–] kogasa@programming.dev 22 points 2 years ago

Have you considered a career in middle management

[–] aksdb@lemmy.world 24 points 2 years ago (2 children)

On microcontrollers that might be a valid approach.

[–] kevincox@lemmy.ml 10 points 2 years ago (1 children)

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

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

[–] aksdb@lemmy.world 2 points 2 years ago

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

[–] Darkassassin07@lemmy.ca 1 points 2 years ago (1 children)

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

[–] YIj54yALOJxEsY20eU@lemm.ee 3 points 2 years ago (1 children)

Microcontrollers run 100% of the time even while sleeping.

[–] towerful@programming.dev 4 points 2 years ago (1 children)

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.

[–] YIj54yALOJxEsY20eU@lemm.ee 3 points 2 years ago* (last edited 2 years ago)

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

[–] Matty_r@programming.dev 12 points 2 years ago (1 children)

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.

[–] henfredemars 12 points 2 years 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.

[–] vcmj@programming.dev 10 points 2 years ago* (last edited 2 years ago)
[–] drmoose@lemmy.world 10 points 2 years ago* (last edited 2 years ago) (1 children)

Javascript enters chat:

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

Which is somehow even worse.

[–] KairuByte@lemmy.dbzer0.com 2 points 2 years ago

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

[–] ExtraMedicated@lemmy.world 5 points 2 years ago (1 children)

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.

[–] snaggen@programming.dev 12 points 2 years 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...

[–] lauha@lemmy.one 4 points 2 years ago

I can relate. We have breaks ate work too.

[–] Bronco1676@lemmy.ml 2 points 2 years 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

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