Live data from Hacker News

Speed up your Python using Rust

developers.redhat.com

21–30 of 102 posts

Re: Speed up your Python using Rust

#21
post #13

I like the article, but the following advice confused me, especially since this comes from RedHat i.e. Linux people: > Having Rust installed (recommended way is https://www.rustup.rs/ ). This essentially recommends unconditionally using the "curl | sh" anti-pattern. Shouldn't they recommend instead e.g. "apt-get install rustc" for Debian users? Since this doesn't make use of too recent Rust features, using Rust 1.14…

You can download the rustup bootstrapper if you don’t like curl to bash. I would recommend against using the debian packages.

Re: Speed up your Python using Rust

#22
post #11
post #10

Earlier quoted context omitted.

why would rust have better performance than cython? cython is as fast as C

Cython just transpiles to C. It's not "as fast as C", it's as fast as the runtime support structures it uses, and the communication with Python allows.

yes, exactly as I said.

Re: Speed up your Python using Rust

#23
post #13

I like the article, but the following advice confused me, especially since this comes from RedHat i.e. Linux people: > Having Rust installed (recommended way is https://www.rustup.rs/ ). This essentially recommends unconditionally using the "curl | sh" anti-pattern. Shouldn't they recommend instead e.g. "apt-get install rustc" for Debian users? Since this doesn't make use of too recent Rust features, using Rust 1.14…

> using Rust 1.14 of Debian/Stable should be fine, shouldn't it? Same of Fedora, etc.

That is a RedHat developer blog. Is rust available on RHEL already? Looking at CentOS (which should have nearly the same packages), it doesn't appear to be available yet.

Re: Speed up your Python using Rust

#24
post #11
post #10

Earlier quoted context omitted.

why would rust have better performance than cython? cython is as fast as C

Cython just transpiles to C. It's not "as fast as C", it's as fast as the runtime support structures it uses, and the communication with Python allows.

You can write very efficient Cython code but it's true that in this case, you tend to adopt a lower level code style that is very close to C/C++. Basically, you need to think about the C/C++ code that will be generated by Cython.

C/C++ compilers might be able to generate more optimized native code than what rutsc does though. Actually, this is a question: how good is rustc with numerical / math intensive code? For instance, does it implements loop unrolling and SIMD vectorization?

Re: Speed up your Python using Rust

#25
post #20
post #13

I like the article, but the following advice confused me, especially since this comes from RedHat i.e. Linux people: > Having Rust installed (recommended way is https://www.rustup.rs/ ). This essentially recommends unconditionally using the "curl | sh" anti-pattern. Shouldn't they recommend instead e.g. "apt-get install rustc" for Debian users? Since this doesn't make use of too recent Rust features, using Rust 1.14…

Rust is a language in active development, continually getting improvements and new features. Using Rustup is the best way of managing up to date toolchains (and multiple toolchain versions if you have to). It's no harder than apt-get install, and sets the best practice early on so that someone doesn't get confused and have to switch later. "curl | sh" is only an anti-pattern in the sense that you have to trust the so…

I agree with your main point, but would like to add that apt does have additional signature verification with gpg, so it's a bit more secure than just https (e.g. anyone with access to a trusted CA and your network can mount an active attack against you).

Re: Speed up your Python using Rust

#26
For comparison, I just implemented the same as C SWIG extension[1]. It's about 10% faster, but it's cheating by comparing bytes instead of utf-8 encoded characters. The more interesting part to me is the comparison of the amount of boilerplate code required.

https://github.com/martinxyz/rust-python-example/commit/f8e3...

Re: Speed up your Python using Rust

#28
About as fast as numpy.. More tools to create fast code is always great, but the tooling for Rust/C in Python needs to be easier, I just can't be bothered most of the time.

This in numpy gets a better relative boost on my machine YMMV.

    import numpy
    def count_double_chars_np(val):
	ng=np.fromstring(val,dtype=np.byte)
	return np.sum(ng[:-1]==ng[1:])

    def test_np(benchmark):
        benchmark(count_double_chars_np, val)

Re: Speed up your Python using Rust

#29
post #26

For comparison, I just implemented the same as C SWIG extension[1]. It's about 10% faster, but it's cheating by comparing bytes instead of utf-8 encoded characters. The more interesting part to me is the comparison of the amount of boilerplate code required. https://github.com/martinxyz/rust-python-example/commit/f8e3...

pytest.benchmark really needs to default to a smaller width of it's stats, those stats are really just meant to be used in a terminal..

Re: Speed up your Python using Rust

#30
post #26

For comparison, I just implemented the same as C SWIG extension[1]. It's about 10% faster, but it's cheating by comparing bytes instead of utf-8 encoded characters. The more interesting part to me is the comparison of the amount of boilerplate code required. https://github.com/martinxyz/rust-python-example/commit/f8e3...

I'm not familiar with Rust libraries, but I would guess it's just counting code points and not characters, so strictly speaking both are cheating.

I would love to see people showing how to do simple string processing, like counting characters in proper grapheme cluster level in their favorite programming language.

Post reply on HN