1. 28

  2. [Comment removed by author]

    1. 10

      The article would be half the length if I had skipped explaining the problem. I don’t see a good way around it. I did include a disclaimer early though. It kinda doesn’t help to explain the faster code if you don’t understand what the slower code did.

      no good advice

      Maybe you can tell me how to make the code faster? What are some general performance tips you commonly use that aren’t listed here.

      This code has to be pure python and isn’t allowed any imports. Let me know if there’s something huge I missed.

      You’re the second person to mention clickbait title. I’m curious what you were expecting when you clicked on the link. It’s about me making a function 5x faster in python when my main language is Ruby. I wrote it to be fairly literal.

      1. 14

        I don’t like the title because it’s phrased in a way that implies you’ve optimized Python code in general, when you’ve really just optimized a specific script. It also implies that it was your Ruby experience specifically that helped you optimize, but most everything you covered would apply in general.

        I’m also always skeptical of blog posts like this that don’t make their full code available. Especially when the author is comparing between two languages, which you’ve implicitly done by constantly mentioning Ruby.

        1. 6

          Interesting. I had never even considered the title would make people think that. Did you think by “some Python code” I meant a specific subset?

          The point of the article is that optimizing code is a general skill that can be applied to other languages. It’s supposed to be advise that applies to both languages.

          Or at least that was what was in my head when I wrote it. I might have not mentioned those words.

          The code I provided in the article is fully executable.

        2. 7

          I’m sure you know this, but for folks who don’t: in your code sample you have, for testing for ‘None’

          if not value:  # <---------- aaaaaaaaaaaaaaaa noooooooooooo!
          my_list = [value]

          it should be

          if value is None:  # <---- this is what you mean
          my_list = [value]
          1. 3

            Good catch, you want to be as specific as possible. The way I used would have caught False in addition to None.

            In ruby would be the difference between

            if value


            if value.nil?
            1. 1

              Mostly. Except that non-nil values can define nil? as true in Ruby ;)

              1. 2

                Though in practice people don’t actually do that. Or rather I’ve never come across it in the wild in the last 10+ years of Ruby.

              2. 1

                also catches 0, [], {} etc. etc. all of which are not None. Importantly, different objects can have different rules as to when they evaluate to be False. I was bitten by this at work

                1. 2

                  There’s also this related bug, which is a fascinating look into the perils of both implementing rules of thumb without thinking about them, and not thinking sufficiently about how time works.

            2. [Comment removed by author]

              1. 3

                if I knew you wrote that article I wouldn’t have made that comment.

                You’re not the only person to make this comment, so I appreciate the response. This is like the rabbit/duck image. I didn’t even realize there was another way of reading the title.

                I used to work phone support at my first job we were always trained “under promise over deliver”. It doesn’t do me any good to make you think I did something AMAZING then when you click the post it was really just okay. So it’s good feedback that the title is ambiguous.

                I tried to workshop the title a bit the best I came up with is “Lifelong Rubyist makes a Python script 5x Faster” still seems equally problematic though.

                I added a note early on to help maybe clarify things earlier hopefully.

                Several people have mentioned they thought the post was about me making the Python interpreter faster. That would have been pretty dang cool, but is not the case. This article is about how to write faster code using an interpreted language.

                1. 2

                  I think the title is fine. It says “some Python code”, not “all Python code”.