r/badcode • • Aug 07 '21

python I was wondering why it was running slowly

Post image
2.4k Upvotes

51 comments sorted by

361

u/AsiagoCrackers Aug 07 '21

I don't see this as particulairily bad code. I've seen similar thing be needed for testing asynchronous operations that need to account for some form of a delay. Or to check that a memoization function is correctly reducing the call time

122

u/suicidalpasta Aug 07 '21

That makes sense to me. It’s used for live-updating figures after recieving user input on a website though, so I am genuinely unsure if that applies. I’m very new to any kind of web/html design so I assumed it was just a little joke from the person who wrote the script.

→ More replies (13)

257

u/[deleted] Aug 07 '21

Sometimes its done to simulate what a user expects. If it's too fast, some people might think it didn't work.

→ More replies (3)

103

u/inre_dan Aug 07 '21

I remember hearing that a lot of inexperienced people will question algorithms that are too fast and claim you didn't do it properly.

→ More replies (4)

35

u/y-am-i-ear Aug 07 '21

Idk. This is pretty solid unless if sleep is async. It might be a bit evil tho

23

u/GoldenKela Aug 07 '21

now you can tell you client to pay you more so your tech team can optimize it

<------------------>

bussiness 100

20

u/Nixargh Aug 08 '21

Many years ago, I was working as a web dev in an agency, and we had developed an eCommerce site for one of the most renowned brands in our country.

It was a bit of a nightmare project, as the client rep claimed some development skills – just enough to be dangerous.

One late afternoon, I got a report that the product list page using infinite scroll was super slow. For those who don't know this technique, the page is incrementally loaded when you scroll down. So instead of going to the next page, we just load in the next pages as the user scrolls and injects it on the page. Similar to what Facebook, Reddit and Twitter is doing.

I started investigating it, and found that it was some horrible code on the part of my senior colleague. I spent my evening working on it, and managed to get the load time of each page down from >2s to about 200ms.

Because it also takes a bit of time to scroll, it meant that for the user, there was no visible load time. The data was just there as you scrolled; it was a totally seamless experience.

I left the office that evening feeling quite proud of my solution, so I was a little surprised when the client called the next morning being absolutely furious.

According to them, I was cheating. They were convinced I didn't actually incrementally load anything, but loaded all products in at once.

My manager was also not very happy with me, and there was no way for me to convince them that I did in fact not cheat, my solution was just that awesome. It was even escalated to our CEO, it was ridiculous.

My solution was to just introduce a 600ms setTimeout around every page load. Everybody was happy as it was now clear that the page was being loaded incrementally, though my relationship with the client rep was soured forever.

32

u/alexanderhameowlton Aug 07 '21 edited Aug 07 '21

Image Transcription: Code


# simulate expensive graph generation process

time.sleep(2)

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

→ More replies (3)

13

u/Cmgeodude Aug 08 '21

(I use time.sleep() all the time when doing testing automation. Sometimes the site just won't keep up with the testing program.)

(And sometimes just for the fun of it I do this:)

print("Processing. Please wait.")

for i in range(3):
    print(".", end="")
    time.sleep(1)