The graph makes no sense. Did a generative AI make it.
Technology
This is a most excellent place for technology news and articles.
Our Rules
- Follow the lemmy.world rules.
- Only tech related news or articles.
- Be excellent to each other!
- Mod approved content bots can post up to 10 articles per day.
- Threads asking for personal tech support may be deleted.
- Politics threads may be removed.
- No memes allowed as posts, OK to post as comments.
- Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
- Check for duplicates before posting, duplicates may be removed
- Accounts 7 days and younger will have their posts automatically removed.
Approved Bots
I think there's a good chance of that:
-2x
instead of~2x
- a human is unlikely to make that mistake- no space here:
==0
- there's a space every other time it's done, including the screenshot - the numbers are wrong - the screenshot has different data than the image
- why are there three bars? A naive approach would have two.
Looks like it. It's a complete fever dream graph. I really don't get how someone can use an image like that. Personally I don't really like AI art anyways, but I could somewhat understand it as a sort of "filler" image to make your article a bit more interesting. But a graph that is supposed to convey actual information? No idea why anyone would AI gen that without checking
yeah I got angry just looking at it
I know I'm gonna get downvoted to oblivion for this, but... Serious question: why use Python if you're concerned about performance?
It's all about trade-offs. Here are a few reasons why one might care about performance in their Python code:
- Performance is often more tied to the code than to the interpreter - an O(n³) algorithm in blazing fast C won't necessarily perform any better than an O(nlogn) algorithm in Python.
- Just because this particular Python code isn't particularly performance constrained doesn't mean you're okay with it taking twice as long.
- Rewriting a large code base can be very expensive and error-prone. Converting small, very performance-sensitive parts of the code to a compiled language while keeping the bulk of the business logic in Python is often a much better value proposition.
These are also performance benefits one can get essentially for free with linter rules.
Anecdotally: in my final year of university I took a computational physics class. Many of my classmates wrote their simulations in C or C++. I would rotate between Matlab, Octave and Python. During one of our labs where we wrote particle simulations, I wrote and ran Octave and Python simulations in the time it took my classmates to write their C/C++ versions, and the two fastest simulations in the class were my Octave and Python ones, respectively. (The professor's own sim came in third place). The overhead my classmates had dealing with poorly optimised code that caused constant cache misses was far greater than the interpreter overhead in my code (though at the time I don't think I could have explained why their code was so slow compared to mine).
Honestly most people use Python because it has fantastic libraries. They optimize it because the language is middling, but the libraries are gorgeous
ETA: This might double post because my Internet sucks right now, will fix when I have a chance
This is my two cents as someone in the industry.
Because, while you don't want to nitpick on each instruction cycle, sometimes the code runs millions of times and each microsecond adds up.
Keep in mind that people use this kind of things for work, serving real world customers who are doing their work.
Yes, the language itself is not optimal even by design, but its easy to work with, so they are making it worth a while. There's no shortage of people who can work with it. It is easy to develop and maintain stuff with it, cutting development cost. Yes, we're talking real businesses with real resource constraints.
Yes, Python is the wrong choice if performance is your top priority.
But here's another perspective: why leave easy performance wins on the table? Especially if the cost is simpler code that works as you probably wanted anyway with both None
and []
?
Python is great if you want a really fast development cycle, because the code is generally quite simple and it's "fast enough." Any wins for "fast enough" is appreciated, because it delays me needing to actually look into little performance issues. It's pretty easy for me to write a simple regex to fix this cose (s/if len\((\w+)\) == 0:/if not \1:/
), and my codebase will be slightly faster. That's awesome! I could even write up a quick pylint
or ruff
rule to catch these cases for developers going forward (if there isn't one already).
If I'm actively tweaking things in my Python code to get a little better performance, you're right, I should probably just use something else (writing a native module is probably a better use of time). But the author isn't arguing that you should do that, just that, in this case, if not foo
is preferred over if len(foo) == 0
for technical reasons, and I'll add that it makes a ton of sense for readability reasons as well.
Here are some other simple wins:
[]
and{}
instead oflist()
anddict()
- the former copy constants, whereas the latter actually constructs things; oh, and you save a few chars- use list comprehensions instead of regular loops - list comprehensions seem to be faster due to not needing to call
append
(and less code) - use built-ins when you can - they're often implemented in native code
I consider each of those cleaner Python code anyway, because they're less code, just as explicit, and use built-in language features instead of reinventing the wheel.
You may want to beneficiate from little performance boost even though you mostly don't need it and still need python's advantages. Being interested in performance isnt always looking for the very best performance there is out of any language, it can also be using little tips to go a tiny bit faster when you can.
It comes down to the question "Is YOUR C++ code faster than Python?" (and of course the reverse).
I've built a SCADA from scratch and performance requirements are low to begin with, seeing as it's all network bound and real world objects take time to react, but I'm finding everything is very timely.
A colleague used SQLAlchemy for a similar task and got abysmal performance. No wonder, it's constantly querying the DB for single results.
Exactly!
We rewrote some Fortran code (known for fast perf) into Python and the net result was faster. Why? They used bubble sort
in a hot loop, whereas we used Python's built-in sort (probably qsort or similar). So despite Python being "slower" on average, good architecture matters a lot more.
And your Python code doesn't have to be 100% Python, you can write performance-critical code in something else, like C++ or Rust. This is very common, and it's why popular Python libraries like numpy and scipy are written in a more performant language with a Python wrapper.
Yea and then you use "not" with a variable name that does not make it obvious that it is a list and another person who reads the code thinks it is a bool. Hell a couple of months later you yourself wont even understand that it is a list. Moreover "not" will not throw an error if you don't use an sequence/collection there as you should but len will.
You should not sacrifice code readability and safety for over optimization, this is phyton after all I don't think list lengths will be your bottle neck.
Strongly disagree that not x
implies to programmers that x
is a bool.
It does if you are used to sane languages instead of the implicit conversion nonsense C and the "dynamic" languages are doing
well it does not imply directly per se since you can "not" many things but I feel like my first assumption would be it is used in a bool context
I would say it depends heavily on the language. In Python, it's very common that different objects have some kind of Boolean interpretation, so assuming that an object is a bool because it is used in a Boolean context is a bit silly.
That's why we use type-hinting at my company:
def do_work(foo: list | None):
if not foo:
return
...
Boom, self-documenting, faster, and very simple.
len(foo) == 0
also doesn't imply it's a list
, it could be a dict
or any other type that implements the __len__
. That matters a lot in most cases, so I highly recommend using type hints instead of relying on assumptions like len(foo) == 0
is probably a list operation.
There are decades of articles on c++ optimizations, that say "use empty() instead of size()", which is same as here.
except for c++ it was just to avoid a single function call, not extra indirection. also on modern compilers size() will get inlined and ultimate instructions generated by the compiler will likely be the same
Isn’t “-2x faster” 2x slower?
That woulb be 0.5x. −2x implies negative duration, which makes no sense. Neither does the layout of anything else in the image.
I don't like it very much, my variable could also be None
here
You'd need to explicitly check for None if using the len() construct as well, so this doesn't change the point of the article.
But None
has no len
if not foo:
-> foo could be an empty list or None
, it is ambiguous.
len(foo)
will lead to an exception TypeError
if foo
is None
, I can cleanly catch that.
It suggests I deal with a boolean when that is not the case. Explicit is better than implicit, and if not foo
to check for an empty list may be pythonic, but it's still implicit af
From that little image, they're happy it takes a tenth of a fucking second to check if a list is empty?
What kind of dorito chip is that code even running on?
I could have tripped, knocked over my keyboard, cried for 13 straight minutes on the floor, picked my keyboard back up, accidentally hit the enter key making a graph and it would have made more sense than this thing.
-2x faster. What does that even mean?
There's probably an "import * from relativity" in there somewhere.
Could also compare against:
if not len(mylist)
That way this version isn’t evaluating two functions. The bool evaluation of an integer is false when zero, otherwise true.