20
atheist
22d

Making python 2x faster by replacing enums with literal values.

Pros, it's faster, cons, it's unreadable.

God I miss compiled languages. At least optimizing them requires intelligent problem solving.

It's a text parser state machine transition so it's a code hot spot, so this kind of optimization is worthwhile. But it's kinda annoying.

Next is get rid of any semblance of readability and replace the match with an array index...

Comments
  • 7
    You could have a preprocessor for the python code. So then you have two versions of a file, one is readable and is used as source, the other is whatever but runs fast(er).

    But then it's kind of self-defeating, as in if one is willing to go that extra mile then a compiled language would be more appropriate in the first place.

    You could maybe write a patch for python itself then, though that's much more involved. Ugh, never a good option.
  • 6
    Take the rust pill...

    You know you want to...
  • 3
    I've always disliked python

    *takes another spiritual swig of spiritual alcohol and feels superior in own opinions* yep
  • 1
    I didn't know Python had enums. I checked and it was added 3.4 apparently. I have not done much coding with Python 3. Did a lot of Python 2.
  • 2
    You, know at one time there was a V8 module for Python... I "think" it had JIT...

    I will see myself out...
  • 8
    The secret trick to make your python code faster is to rewrite your code in C++ or another compiled language! Or at least parts of it via linking.
  • 4
    @Hazarth Pythonicists hate this one neat trick...
  • 3
    @Demolishun 3.13 has actually added a JIT. And real multithreading!
  • 2
    @12bitfloat I think you'll find, c++. The arc stuff seems too verbose, tedious and superfluous to me...

    Although there is a c++ proposal for safe memory handling. So maybe in 2030…
  • 2
    @Hazarth "parts of it via linking" yeah... It's just such a pain in the ass to set up. Have you ever tried reading numpy's build system? Fuck that for a laugh.
  • 5
    @Hazarth rewriting in c++ however would make it run faster but way slower to develop. I'm just as experienced with c++ as python, but python is batteries included. Rust, a bit better with cargo but... Rust...
  • 3
    @atheist holy fucking hallelujah!!!

    note: referencing the jit and threading
  • 3
    @atheist I don't know for the JIT but for the real threading you have to compile yourself with the disable gil flag enabled or smth.

    Yeah, afaik C++ will get the borrow thing or smth.

    It would actually be nice to rewrite your library in C right when it's completed. If it's all thought trough, it should be doable right?

    @12bitfloat Rust is a funny dinky toy language only good for writing pygame-like alternatives.

    In exception of tsoding, I've never heard someone using c3. c3 is a C alternative. A bit better is the idea. I watched tsoding implementing websockets in it today. C3 didn't had a websocket library yet
  • 1
    @retoor I'm using pyenv which has a rebuilt threading build so pretty easy. I've also been using a dev 3.14 version for ages because it's got some really nice typing improvements 😁
  • 1
    @retoor rewriting in c would be doable but I'm using pandas to parse the utf spec sheets so that supporting new utf versions (that haven't changed rules) is just a case of adding the new spec sheets. But I'm doing it so I can do restructuredtext parsing which would just not be fun to rewrite in c. Maybe c++ but it's all string manipulation which is just a bit easier in python.
  • 1
    @retoor zig is on my list of languages of interest.
  • 3
    @atheist tsoding is total fan of zig. That promises something. I didn't try it yet. I have too many projects to learn a language on top of. Since C remains fastest, i keep using that. I was busy learning Rust with minimal effort by watching a 3hr video about it. Watched half, lost interest since I knew i'm not gonna use it. I just wanted to be up-to-date. That's not a good motivation and imo you don't really learn coding from video. At most some concept.

    I did learn wren not long ago, very easy js like language and its source code itself is beautiful. I'm a fan, but the language is abandoned. Not the first time this maker abandoned a language. Sad, i wrote networking capabilities for it. The socket server with my lib was as fast as python's async server with heavy load testing using apache benchmark. Still fun project
  • 1
    @atheist have you tried Go? I find It's almost as intuitive to use as Python, but it is a compiled langugage and It's perfect for multi threading
  • 1
    Just use Swift. It’s as easy and convenient as python but is compiled and has ARC instead of GC, which is better for performance.

    No need to make compromises with C++, Rust or Go.
  • 1
    if you feel the need to stay within the language, try ironpython.
  • 2
    Python isn't for speed critical tasks. And most tasks aren't speed critical.

    I've been writing things in python unless I need speed, then relying on c++, and I haven't had to touch c++ in 6 or 7 years. Computers are fast now.
  • 1
    @lungdart "python isn't for speed critical tasks" if all you've got is a hammer then everything is a nail. I think c++ isn't even about speed, it's about customization. You can probably make python nearly as fast as c++ in 90% of use cases because of numpy etc (not counting them as compiled). Customized c++ is only really necessary when you need so much fine control that you can't use them.
  • 2
    Write it in zig, generate a shared library, call from Python. Easy!
  • 0
    @atheist it's all about delivering value.

    If I can write something in an hour in python or a day in c, I'll pick Python.

    And the reason you want fine grained control over things when using c/cpp is to optimize for something. Usually speed, but sometimes disk, memory, or bandwidth.
  • 0
    @AlgoRythm "Easy!" have you tried it recently? The native build process isn't well supported by newer dependency management tools (poetry it's a completely undocumented feature I gave up trying to get to work). For pdm there at least looks to be someone that got it working recently https://rgoswami.me/posts/...
    but even then, if I'm writing python is like it to be cross platform so there's the cross compilation hell to deal with too...
  • 0
    @atheist I’ve never used SOs and Python before, I assumed it would at least be well supported
  • 0
    @AlgoRythm ahahahaha no it is not. Cython documentation is terrible...
  • 2
    @atheist python issue 👎 it should be as simple as an import statement, and the VM should search automatically for a .DLL/.LIB file on Windows platforms and a .SO / .A files on *nix systems.
  • 0
    @AlgoRythm it "works" nearly flawlessly. But getting it working is like running into a brick wall. We have a work package with python bindings and it's a cmake build system and that's probably the best way to do it. The Python build systems are somewhat lacking for doing something quickly.
  • 0
    A what?
  • 0
    @kiki it's now explicitly a regex so a regex. Regexes are state machines.
Add Comment