this post was submitted on 12 Apr 2025
195 points (90.1% liked)

Technology

68918 readers
4172 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. 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.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
top 50 comments
sorted by: hot top controversial new old
[–] Opisek@lemmy.world 28 points 6 days ago (5 children)

The graph makes no sense. Did a generative AI make it.

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.
[–] gerryflap@feddit.nl 5 points 5 days ago

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

[–] pyre@lemmy.world 4 points 5 days ago

yeah I got angry just looking at it

load more comments (2 replies)
[–] PattyMcB@lemmy.world 52 points 6 days ago (9 children)

I know I'm gonna get downvoted to oblivion for this, but... Serious question: why use Python if you're concerned about performance?

[–] lengau@midwest.social 55 points 6 days ago (8 children)

It's all about trade-offs. Here are a few reasons why one might care about performance in their Python code:

  1. 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.
  2. Just because this particular Python code isn't particularly performance constrained doesn't mean you're okay with it taking twice as long.
  3. 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).

load more comments (8 replies)
[–] JustAnotherKay@lemmy.world 14 points 6 days ago* (last edited 6 days ago) (4 children)

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

load more comments (4 replies)
[–] pastermil@sh.itjust.works 11 points 6 days ago* (last edited 6 days ago) (2 children)

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.

load more comments (2 replies)
[–] sugar_in_your_tea@sh.itjust.works 6 points 5 days ago* (last edited 5 days ago)

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 of list() and dict() - 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.

[–] Takapapatapaka@lemmy.world 8 points 6 days ago

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.

[–] Randelung@lemmy.world 4 points 6 days ago (1 children)

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.

load more comments (3 replies)
[–] iAvicenna@lemmy.world 28 points 6 days ago* (last edited 6 days ago) (12 children)

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.

[–] jerkface@lemmy.ca 15 points 6 days ago (7 children)

Strongly disagree that not x implies to programmers that x is a bool.

[–] taladar@sh.itjust.works 7 points 6 days ago

It does if you are used to sane languages instead of the implicit conversion nonsense C and the "dynamic" languages are doing

[–] iAvicenna@lemmy.world 6 points 6 days ago (5 children)

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

[–] thebestaquaman@lemmy.world 8 points 6 days ago (10 children)

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.

load more comments (10 replies)
load more comments (4 replies)
load more comments (5 replies)
[–] sugar_in_your_tea@sh.itjust.works 3 points 5 days ago* (last edited 5 days ago) (8 children)

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.

load more comments (8 replies)
load more comments (10 replies)
[–] uis@lemm.ee 16 points 6 days ago (1 children)

There are decades of articles on c++ optimizations, that say "use empty() instead of size()", which is same as here.

[–] dreugeworst@lemmy.ml 5 points 6 days ago

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

[–] ne0n@lemmy.world 7 points 6 days ago (5 children)

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.

load more comments (4 replies)
[–] gigachad@sh.itjust.works 6 points 6 days ago (1 children)

I don't like it very much, my variable could also be None here

[–] iknowitwheniseeit@lemmynsfw.com 3 points 6 days ago (1 children)

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.

[–] gigachad@sh.itjust.works 6 points 6 days ago* (last edited 6 days ago) (10 children)

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

load more comments (10 replies)
[–] AnUnusualRelic@lemmy.world 2 points 4 days ago* (last edited 4 days ago)

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?

[–] Harvey656@lemmy.world 3 points 5 days ago (1 children)

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?

[–] AnUnusualRelic@lemmy.world 2 points 4 days ago

There's probably an "import * from relativity" in there somewhere.

[–] antlion@lemmy.dbzer0.com 7 points 6 days ago (4 children)

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.

load more comments (4 replies)
[–] knighthawk0811@lemmy.ml 7 points 6 days ago (1 children)

so these are the only 2 ways then? huge if true

load more comments (1 replies)
load more comments
view more: next ›