r/badcode • • Jul 29 '21

python Was doing some bug hunting and found essentially what was a sum to n function but of O(fuck) time complexity

Post image
1.2k Upvotes

80 comments sorted by

View all comments

Show parent comment

5

u/akren1 Jul 30 '21 edited Jul 30 '21

I think I see your point, but I don't really agree.

I was referring to the "theory" as a "non practicall use".

What I believe you understand as a theory is complexity theory abstracted out of Python, or any given language. But even in such abstraction it is not O(1) when you consider bugnums (don't limit domain is N).

So it is not O(1) for domain of all natural numbers which is valid domain.

In Python the + or * operation are simply not an single operations.

Taking all dynamic typing out of the way, and just focusing on integers it is a function call.

And if we don't limit domain of n, that function tme/space complexity if not O(1). I believe a+b is O(log ab), and * can depend on used algorithm.

It can by O(1) if we constraint N by int32, int64, or even int_21000. But for arbitrarily large numbers complexity depends on a number, not just amount of them. Given the python code I would assume such numbers even if we abstract out of python.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

1

u/akren1 Jul 30 '21 edited Jul 30 '21

But you can't analyse algorithms with arbitrarily large numbers as inputs, without includind algorithms for arithmetics operation into consideration. This is not an implementation detail. This is computational theory of algorithms that work with bignums. And there are a lot of algorithms that are known in this field.

What if instead of a+b we write: BignumAdd(a, b). You can assume it is O(1), but such algorithms does not exist. You can mark it as unknown - f(a,b) and analyse from there if you want to abstract out of "implementation details". But that is no implementation, that is bignum arithmetics.

Abstrat machines that can do such operations in O(1) are theoretically studied, and you can assume we work with one like that. But given the context such assumption should be explicit.

And on the side note. If I analyse time complexity on a given runtime, it does depend on the runtime. Built in functions/features have it's complexity. And the complexity might be different on different runtime.

Edit:

It's like saying: std::fill is O(1) when we analyse time complexity of C++ program - it's just not. And we must assume something when we analyse complexity of entrie algorithm.

What if stl got an update next week that makes std::fill run in O(1) - it would change complexity of our algorithm.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

1

u/WinstonCaeser Jul 30 '21

"There is nothing intrinsically linear about the runtime complexity of the mathematical calculation  x * (x + 1) / 2 . You have one input, no recursion, no looping, nothing. It operates in O(1). Theoretically speaking, of course."

There is, intrinsically, you must read every digit making it at least O(num digits).

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 30 '21

I keep saying digits, not saying the base. No matter what base you represent it in the size scales with the number. Be it base 2(binary) or base 264.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 31 '21

Bruh, idk how many times I have to explain this to you.

As I said from the very beginning, it depends if your base unit is a number or a more precisely a digit.

INTRINSICALLY you must read all digits, therefore it is At least O(num digits) for any number, you must read and operate on each of its digits.

You are stuck on evaluating the implementation that most architectures limit multiplication to a fixed number of digits and operate assuming worst case.

In THEORY multiplication is at least O(num digits). In practice, many implementations limit multiplication to a fixed number of digits making it O(1).

It is often extremely important when operating with matrices to consider the cost of multiplication.

Particularly for simple functions, like the above, where the cost of multiplication is not dominated by some other part of the function, it becomes important to factor in the cost.

0

u/[deleted] Jul 31 '21

[deleted] — view deleted comment

→ More replies

1

u/akren1 Jul 30 '21

You've just agreed with me. I said what you did in your reasoning. But I said that given the context such assumption should be explicit. You didn't make it explicit. And just as machines that do Bignums in O(1), the ones that do not are also studied.

My point was that from the theoretical standpoint, things like BigNum don't exist- you only have numbers.

They exist - it's a hole field of both theoretical and practical study. Just as for example both theoretical and practical study of matrix multiplication.

From the theoretical standpoint BigNums exist just like matrices do. You either analyze number in given range, or arbitrarily large. And when you analyze arbitrarily large numbers you need some assumptions.

Again, that's the point I've been making. If your STL hashmap does its lookups in linear time, you'll never reap the benefits of an actual O(1) lookup. However, that doesn't mean hashmaps don't have a theoretical O(1) lookup, it means there's an implementation quirk to be aware of in that language.

Bignums don't have theoretical O(1) arithmetic - it can be proven pretty easily.

There is nothing intrinsically linear about the runtime complexity of the mathematical calculation x * (x + 1) / 2. You have one input, no recursion, no looping, nothing. It operates in O(1). Theoretically speaking, of course.

Just as you don't see looping in std::fill. But it is there - it is just hidden by pretty operator. Maybe it is not intrinsic, but that's why there are entire theories/algorithms about it.

Again- your point was never wrong, you just mislabeled the theoretical limit portion.

Neither is yours in logical way. This more of an "definitions" dispute. But with that in mind my claims stand unchanged.