treyhunner,
@treyhunner@mastodon.social avatar

Self-taught programmers: what made the idea of "time complexity" really click for you? 🧮

Was there a video, blog post, book chapter, etc. that really acted as an ah-ha moment? 💡

#Python #programming

spencer,
@spencer@nrw.social avatar

@treyhunner
I still do struggle with the exact meaning of O(log n) but I actually did not come into a (hobby) situation where it did actually matter. O(1), O(n) or O(n^2) made click pretty fast. In fact they were nifty terse symbols for situations I ran (hit the bottom) into earlier.
OT: A bigger mental impact was the realization that SQL is “just” set-theory. Now sets are everywhere …

treyhunner,
@treyhunner@mastodon.social avatar

@spencer interestingly O(log n) I have intuition about and can explain but O(n log n) I struggle with.

Here's my log n explanation: I'm thinking of a number from 1 to 100. After you try to guess my number I'll tell you either "correct", "lower", or "higher". Your most sensible first guess is 50. Then it's either 25 or 75 (depending on whether I said lower or higher) and so on.

You will always guess correctly within 7 guesses.

If it were 1 to 10,000 you'd guess within about 20.

That's log n.

spencer,
@spencer@nrw.social avatar

@treyhunner THIS is a very good explanation with an example algorithm I even used recently. So it's like ceil(log(n, 2)). Roughly said finding a value within a binary tree usually has a complexity of O(log n)!?

treyhunner,
@treyhunner@mastodon.social avatar

@spencer yes!

Binary search is the classic example of O(log n).

When your search space narrows down by a percentage of your data (rather than a fixed amount) for each step, you're in logarithm land.

(very late reply because my app didn't show me a notification on your reply!)

treyhunner,
@treyhunner@mastodon.social avatar

I think I should clarify what I'm asking:

When did the practical relevance of time complexity really click for you?

Meaning, when did you realize that this style of "thinking in orders of magnitude" is helpful for avoiding N+1 problems or other unnecessary loops (or something even deeper if you've needed to care/think about n log n, log n, etc.)?

pg,
@pg@hci.social avatar

@treyhunner @gkapfham -- hi again trey! (thanks greg for the boost) ... there should be a term when you re-find a colleague on mastodon whom you haven't heard from in years since you quit twitter

treyhunner,
@treyhunner@mastodon.social avatar

@pg @gkapfham hey Philip! 👋

There should be a term for that. 😆

Hope you've been well. Would love to catch up sometime.

acdha,
@acdha@code4lib.social avatar

@treyhunner counting cycles in assembly really drove that home. It’s one thing to know something is faster but another to see it being the difference between completing shortly and hogging the 4MHz PC for an hour.

Also back then they weren’t blog posts but text files shared on bulletin boards 😁

treyhunner,
@treyhunner@mastodon.social avatar

@acdha So share text files over bulletin boards. Got it! ✅ 😆

acdha,
@acdha@code4lib.social avatar

@treyhunner Maybe a more defensible point is “use really slow computers handed down from your relatives so you can't afford not to think about performance”?

Come to think of it, I want everyone building front-end websites to use their grandfather's old budget Android phone.

mistersql,
@mistersql@mastodon.social avatar

@treyhunner It's just algebra, right? I don't know how it would click with anyone who hasn't invested the time to get a feel for how algebra works.

I'm still waiting for a time in my career when big O notation thoughts are useful. Performance problems are rarely related to low level algorithms. What is the big O/time complexity of not realizing you need to turn off culture-aware comparisons to get a 1000x speedup in C# string handling? Or using orjson instead of import json?

acdha,
@acdha@code4lib.social avatar

@mistersql @treyhunner the one area it’s come up most for me professionally has been using SQL with ORMs: the difference between O(1) vs O(n) is hard to miss when the times are network call level.

(I’m also reminded of a certain architect who designed an SPA to do XHRs in a nested loop and then announced that the network was too slow)

treyhunner,
@treyhunner@mastodon.social avatar

@acdha @mistersql This is what I was thinking too.

The N+1 problem with DB queries is the most relevant manifestation of a time complexity issue in web development.

In general, nearly all of the time complexity-related optimizing I've needed was a matter of either:

Turn O(n^2) into O(n) by removing a loop

Avoid O(n) when O(1) is easy, especially when the operation might be looped as well.

And sometimes reducing the need for even heavier looping, like O(n^3) with caching or something else.

manlycoffee,
@manlycoffee@techhub.social avatar

@treyhunner

I'll need to ask you a question: what are you struggling with?

Time complexity just made sense to me.

Hard-coded procedure? O(1)

For-loop? O(n)

Loops-in-a-loop? O(n^m) (where m is the number of loops)

That and among other things.

What do you need help with?

treyhunner,
@treyhunner@mastodon.social avatar

@manlycoffee I don't need help with understanding it. I'm trying to understand what's worked for others.

I learned time complexity in a CS degree where I needed to internalize it to pass the class. For those working in a non-academic setting, I wonder where the typical struggle is.

I'm so far thinking it's in recognizing the real word application. Seeing O(n^2) code that loops short and efficient but implicitly causes a loop-within-a-loop and realizing noting the speedup when moving to O(n).

raiderrobert,
@raiderrobert@mastodon.social avatar

@treyhunner a live demo of different sorting algorithms by a professor. He let the algorithms actually run, so he used real time to teach.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • magazineikmin
  • normalnudes
  • tacticalgear
  • Durango
  • rosin
  • Youngstown
  • slotface
  • InstantRegret
  • mdbf
  • kavyap
  • ethstaker
  • thenastyranch
  • khanakhh
  • Leos
  • ngwrru68w68
  • anitta
  • cisconetworking
  • love
  • everett
  • modclub
  • osvaldo12
  • GTA5RPClips
  • provamag3
  • cubers
  • tester
  • megavids
  • JUstTest
  • All magazines