sh.itjust.works
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
JPDev@programming.dev to Programmer Humor@programming.dev · 1 year ago

Sleep() at home

programming.dev

message-square
32
link
fedilink
587

Sleep() at home

programming.dev

JPDev@programming.dev to Programmer Humor@programming.dev · 1 year ago
message-square
32
link
fedilink
alert-triangle
You must log in or register to comment.
  • EisFrei@lemmy.world
    link
    fedilink
    arrow-up
    90
    ·
    1 year ago

    Ah yes. The speedup-loop.
    https://thedailywtf.com/articles/The-Speedup-Loop

    • tryptaminev 🇵🇸 🇺🇦 🇪🇺@feddit.de
      link
      fedilink
      arrow-up
      23
      ·
      1 year ago

      This is brilliant.

  • bangupjobasusual
    link
    fedilink
    arrow-up
    85
    ·
    1 year ago

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

    • ryannathans@aussie.zone
      link
      fedilink
      arrow-up
      28
      ·
      1 year ago

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

      • vrighter@discuss.tchncs.de
        link
        fedilink
        arrow-up
        5
        ·
        1 year ago

        asm(“nop”);

    • Midnight Wolf@lemmy.world
      link
      fedilink
      English
      arrow-up
      17
      ·
      1 year ago

      Homer: “oh yeah speed holes sleep”

      • jaybone@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Sleep holes

  • Darkassassin07@lemmy.ca
    link
    fedilink
    English
    arrow-up
    69
    ·
    1 year ago

    Tell the CPU to wait for you?

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

    • jaybone@lemmy.world
      link
      fedilink
      arrow-up
      27
      ·
      1 year ago

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

      • leclownfou
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

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

    • Kogasa@programming.dev
      link
      fedilink
      arrow-up
      23
      ·
      1 year ago

      Have you considered a career in middle management

  • aksdb@lemmy.world
    link
    fedilink
    arrow-up
    26
    ·
    1 year ago

    On microcontrollers that might be a valid approach.

    • kevincox@lemmy.ml
      link
      fedilink
      arrow-up
      11
      ·
      1 year 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.

      • aksdb@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

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

    • Darkassassin07@lemmy.ca
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      But then I gotta buy a space heater too…

      • YIj54yALOJxEsY20eU@lemm.ee
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        Microcontrollers run 100% of the time even while sleeping.

        • towerful@programming.dev
          link
          fedilink
          arrow-up
          5
          arrow-down
          1
          ·
          1 year 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.

          • YIj54yALOJxEsY20eU@lemm.ee
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            1 year ago

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

  • Matty_r@programming.dev
    link
    fedilink
    arrow-up
    12
    ·
    1 year 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.

    • henfredemars@infosec.pub
      link
      fedilink
      English
      arrow-up
      11
      ·
      1 year 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
    link
    fedilink
    arrow-up
    10
    ·
    edit-2
    1 year ago

    Its a thing. https://en.m.wikipedia.org/wiki/Busy_waiting

  • Dr. Moose@lemmy.world
    link
    fedilink
    English
    arrow-up
    11
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Javascript enters chat:

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

    Which is somehow even worse.

    • sbv
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

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

    • KairuByte@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

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

  • ExtraMedicated@lemmy.world
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year 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.

    • snaggen@programming.dev
      link
      fedilink
      arrow-up
      13
      ·
      1 year 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…

      • ferret
        link
        fedilink
        English
        arrow-up
        3
        ·
        1 year 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

        • snaggen@programming.dev
          link
          fedilink
          arrow-up
          5
          ·
          1 year 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

  • Socsa
    link
    fedilink
    arrow-up
    4
    ·
    1 year ago

    You gotta measure the latency of the first loop.

  • aname@lemmy.one
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    I can relate. We have breaks ate work too.

  • Bronco1676@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    1 year 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

  • hexaflexagonbear [he/him]@hexbear.net
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    deleted by creator

  • No_Ones_Slick_Like_Gaston@lemmy.world
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    1 year ago

    Sudo sleep

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

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

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1.03K users / day
  • 4.68K users / week
  • 9.01K users / month
  • 17.5K users / 6 months
  • 1.2K local subscribers
  • 23.1K subscribers
  • 1.38K Posts
  • 49.8K Comments
  • Modlog
  • mods:
  • Feyter@programming.dev
  • adr1an@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org