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

Technology

68772 readers
3864 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
[–] AnUnusualRelic@lemmy.world 2 points 1 day ago* (last edited 1 day 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?

[–] Opisek@lemmy.world 28 points 2 days ago (5 children)

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

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

yeah I got angry just looking at 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 2 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

load more comments (2 replies)
[–] thebestaquaman@lemmy.world 107 points 3 days ago (4 children)

I write a lot of Python. I hate it when people use "X is more pythonic" as some kind of argument for what is a better solution to a problem. I also have a hang up with people acting like python has any form of type safety, instead of just embracing duck typing.This lands us at the following:

The article states that "you can check a list for emptiness in two ways: if not mylist or if len(mylist) == 0". Already here, a fundamental mistake has been made: You don't know (and shouldn't care) whether mylist is a list. These two checks are not different ways of doing the same thing, but two different checks altogether. The first checks whether the object is "falsey" and the second checks whether the object has a well defined length that is zero. These are two completely different checks, which often (but far from always) overlap. Embrace the duck type- type safe python is a myth.

[–] sugar_in_your_tea@sh.itjust.works 5 points 2 days ago (6 children)

type safe python is a myth

Sure, but type hints provide a ton of value in documenting for your users what the code expects. I use type hints everywhere, and it's fantastic! Yes, there's no guarantee that the types are correct, but with static analysis and the assumption that your users want their code to work correctly, there's a very high chance that the types are correct.

That said, I lie about types all the time. For example, if my function accepts a class instance as an argument, the intention is that the code accept any class that implements the same methods as the one I've defined in the parameter list, and you don't necessarily have to pass an instance of that class in (or one of its sub-classes). But I feel like putting something reasonable in there makes a lot more sense than nothing, and I can clarify in the docstring that I really just need something that looks like that object. One of these days I'll get around to switching that to Protocol classes to reduce type errors.

That said, I don't type hint everything. A lot of private methods and private functions don't have types, because they're usually short and aren't used outside the class/file anyway, so what's the point?

load more comments (6 replies)
load more comments (3 replies)
[–] PattyMcB@lemmy.world 53 points 3 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 3 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 2 days ago* (last edited 2 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)
[–] sugar_in_your_tea@sh.itjust.works 6 points 2 days ago* (last edited 2 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.

[–] pastermil@sh.itjust.works 11 points 2 days ago* (last edited 2 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)
[–] Randelung@lemmy.world 4 points 2 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 (4 replies)
[–] sirber@lemmy.ca 42 points 3 days ago* (last edited 3 days ago) (14 children)

How does Python know if it's my list or not?

[–] 2xsaiko@discuss.tchncs.de 27 points 3 days ago
load more comments (13 replies)
[–] uis@lemm.ee 16 points 3 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 2 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 2 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)
[–] Harvey656@lemmy.world 3 points 2 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 1 day ago

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

[–] iAvicenna@lemmy.world 28 points 3 days ago* (last edited 3 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.

[–] sugar_in_your_tea@sh.itjust.works 3 points 2 days ago* (last edited 2 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)
[–] jerkface@lemmy.ca 15 points 3 days ago (22 children)

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

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

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

[–] iknowitwheniseeit@lemmynsfw.com 3 points 2 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 2 days ago* (last edited 2 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)
load more comments
view more: next ›