News for hackers. Stuff that matters.
Re:“Clean” Code, Horrible Performance by magicalhippo on Tuesday February 28, 2023 @05:20PM (#34975740)
Then again, thing's aren't in the critical path until suddenly they are.

Regardless of scenario I will never willingly do a O(n^2) sort when writing new code. Just in case those 10 items suddenly turn to 10000 one day.

Re:“Clean” Code, Horrible Performance by joaonmatos on Tuesday February 28, 2023 @06:32PM (#34976471)
Are you even manually implementing sorts frequently?

Even languages that are notorious for having tiny libraries, like C and JS, have built-in sorts.

Re:“Clean” Code, Horrible Performance by mattgreenrocks on Tuesday February 28, 2023 @07:37PM (#34977136)
It’s less about accidentally writing n^2 sorts and more about not accidentally creating n^2 algorithms.
Re:“Clean” Code, Horrible Performance by whstl on Tuesday February 28, 2023 @10:16PM (#34978337)
Not sorting, but very simple algorithms that can't afford having O(n^2) performance? That's common even in CRUD apps.
Re:“Clean” Code, Horrible Performance by dagw on Wednesday March 01, 2023 @04:42AM (#34980474)
Perhaps not sort so much, but certainly with search I've seen people roll their own inefficient search functions many times.
Re:“Clean” Code, Horrible Performance by joaonmatos on Sunday March 05, 2023 @05:11AM (#35028300)
It is the exact same situation, though. Most people can just chain a sort and a binary search to do it, and both are included in most languages. Or just put it into a tree map, if your language has it.
Re:“Clean” Code, Horrible Performance by bcrosby95 on Tuesday February 28, 2023 @06:34PM (#34976490)
The moon doesn't fall into the ocean until it does.
Re:“Clean” Code, Horrible Performance by TeMPOraL on Tuesday February 28, 2023 @07:32PM (#34977100)
Not a fair comparison :P.

The point is that the developers may think O(n^2) is fine because their toy use cases had n=10...100, but then actual users will try to use the software for n=10k, or n=100k, and then either waste their lives working with suddenly slow software, or look for alternatives.

I walked into a case like this the other day. I wanted to do a little semi-collaborative project planning. I found a nice tool, played with it for a moment, figured it has the functionality I need and it's fast enough. Then decided to do the actual plan. Once the number of entries in the system went from 10-20 to 30-40, I started to feel things get a little laggy. 50-60, more laggy. At this point I was committed, so I suffered the tool for couple of months, as its UI kept breaking when handling 100 entries. If I knew this would happen at the start, I'd look for something else. But instead, I walked into a hidden O(n^2) somewhere, that makes me hate the product with a passion now.

Re:“Clean” Code, Horrible Performance by chenglou on Tuesday February 28, 2023 @08:57PM (#34977781)
It's more than that. The way black box composition is done in modern software, your n=100 code (say, a component) gets reused into a another thing somewhere above, and now you're being iterated through m=100 times. Oops, now n=10k

Generally, Casey seems to preach holistic thinking, finding the right mental model and just write the most straightforward code (which is harder than it looks; people get distracted in the gigantic state space of solutions all the time). However this requires 1. a small team of 2. good engineers. Folks argue that this isn't always feasible, which is true, but the point of these presentations is to spread the coding patterns & knowledge to train the next gen of engineers to be more aware of these issues and work toward said smaller team & better engineers direction, knowing that we might never reach it. Most modern patterns (and org structures) don't incentivize these 2 qualities.

Re:“Clean” Code, Horrible Performance by Akronymus on Wednesday March 01, 2023 @04:44AM (#34980488)
> The way black box composition is done in modern software, your n=100 code (say, a component) gets reused into a another thing somewhere above, and now you're being iterated through m=100 times. Oops, now n=10k

That doesn't seem quite right. as 100 * (100^2) <<<<< 10000^2

Re:“Clean” Code, Horrible Performance by chenglou on Wednesday March 01, 2023 @10:55PM (#34991808)
Yeah I was only talking about quantities. Equivalently, assume that it's a linear algorithm in the child and a linear one in the parent. Ultimately it ends up as O(nm) being some big number, but when people do runtime analysis in the real world, they don't tend to consider the composition of these blackboxes since there'd be too many combinations. (Composition of two polynomial runtimes would be even worse, yeah.)

Basically, performance doesn't compose well under current paradigms, and you can see Casey's methods as starting from the assumption of wanting to preserve performance (the cycles count is just an example, although it might not appeal to some crowds), and working backward toward a paradigm.

There was a good quote that programming should be more like physics than math.

Re:“Clean” Code, Horrible Performance by celrod on Tuesday February 28, 2023 @10:06PM (#34978282)
There was a fun example in the Julia compiler around a year ago.

Part of the compiler was O(N^2) in `let` block nesting depth. That is

  let x = foo(), y = y, z = 2y
    ...
  end
would be a depth of 3. It didn't seem like that should be a problem, N is never going to be 10, let alone 100, right?

Until suddenly, `N` was in the thousands in some critical generated code spit out by some modeling software, so that handling the scoping introduced by `let` suddenly dominated the compilation time...

Re:“Clean” Code, Horrible Performance by bcrosby95 on Tuesday February 28, 2023 @10:40PM (#34978499)
Your statement was tautological. It applies to literally anything. It's pointless statement.

As far as fair comparisons go, the whole context of this thread is optimization, not algorithms. The OP is about loop unrolling and not using virtual functions or even functions at all. This is not anywhere close to the same as algorithm choice.

Re:“Clean” Code, Horrible Performance by cturner on Wednesday March 01, 2023 @03:34AM (#34980062)
"Regardless of scenario" limit your options.

If you are shipping a binary to your users that will never be able to get updates, your cautiousness would be justified. There are other situations where it will needlessly limits options.

There are situations where I have knowingly written O(n^2) or worse, and put a # xxx dragons marker by it. Quick to write, leave my options open, keep my momentum on the problem I care about.

I will grep for xxx issues at some later time. I may end up throwing out the code before that happens. If I hit big-o issues before then, I can refactor.

I once had a system where the important problems turned out to be a series of IO bottle-necks - nothing to do with computation - but that was obscured because good sense had been burnt at the altar of compute efficiency.