Live data from Hacker News

Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

benhoyt.com

151–160 of 234 posts

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#151
post #150

I'm curious why go is faster than rust in the benchmarks. My best theory is that the program doesn't run long enough to trigger a GC, so rust is paying the cost of freeing memory, but go isn't. (Using an arena allocator in rust would probably produce similar behavior). Or maybe the difference is just noise? It's hard to tell without more details on how the benchmarks were run.

Most of the time you see Rust lose on that kind of benchmark due to it not using buffered algorithm in the standard library (buffers are available and easy to use but they are an option you have to think about and search for).

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#152
post #12

I would not call it performance comparison at all. When Python call functions written in C it is not Python's performance. Write those functions using plain Python and then see the results. Sure for this basic example in the article it does not matter from a practical standpoint. But when you need to step away from canned cases suddenly Python's performance sucks big time.

If it looks like python and quacks like python...

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#153

Curious if anyone knows why the swift version is slow? Is it the casting from subsequence to a string?

Here's a very rough breakdown of the operations it does:

  1.33 s    56.7% specialized Collection.split(separator:maxSplits:omittingEmptySubsequences:)
  263.00 ms 11.2% Substring.lowercased()
  226.00 ms  9.6% specialized Dictionary.subscript.modify
  189.00 ms  8.0% readLine(strippingNewline:)
As you can see, calling split on the string is really slow. The reason it is slow is that it's using the generic implementation from Collection, rather than String, which doesn't really know anything about how String works. So to do the split it's doing a linear march down the string calling formIndex(after:), then subscripting to see if there's a space character there using Unicode-aware string comparison on that one Character.

Swift is really nice that it gives you "default" implementations of things for free if you conform to the right things in the protocol hierarchy. But, and this is kind of unfortunately a common bottleneck, if you "know" more you should really specialize the implementation to use a more optimized path that can use all the context that is available. For example, in this case a "split" implementation should really do its own substring matching and splitting rather than having Collection call it through its slow sequential indexing API. Probably something for the stdlib to look at, I guess.

The rest of the things are also not too unfamiliar but I'll go over them one by one; lowercased() is slow because it creates a new Substring and then a new String, plus Unicode stuff. Dictionary accesses are slow because Swift uses a secure but not very performant hash function. readLine is slow because the lines are short and the call to getline is not particularly optimized on macOS, reallocs and locks internally, then the result is used to create a new String with the newline sliced off.

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#154

Earlier quoted context omitted.

I agree that the benchmark should be run for much longer to get a useful result. But why does Swift have a long startup time in the first place? Shouldn’t it start near instantly like the C and Rust programs?

I don't think Swift's performance is due to start up time at all. I actually cloned the repo, and ran the benchmark and found that Swift's execution time scales drastically with the size of the input. The Swift team actually boasts about its quick start up time on the official website [1]. I ran a simple hello world benchmark to gauge Swift's start up time and got an output of 13 milliseconds. echo 'print("hello worl…

Yeah the startup time for the Swift code is on the order of several milliseconds on my computer, the profiler running at 1000 Hz only gets four samples off of it

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#155
post #17

Wow. Swift, touted as "safe by design and (...) runs lightning-fast"[1] is more of a screw-up than I thought. Almost twice as slow as Lua and behind even Pascal and Forth. [1] https://developer.apple.com/swift/

I only skimmed the article, but I suspect they are including the time necessary to spin up the executable and any runtime environment prior to executing the relevant code. "Lightning fast" can mean a lot of things, and it might not mean "executables start quickly."

Swift executables start fairly quickly.

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#156
post #75
post #62

Some of these efficiency gains are remarkable. Most of the code I write is just the quickest way to write a mundane function. Really curious if you went into a huge tech co and devoted ~10% of SWE time solely to optimization of code base what the energy savings would be.

You can't walk into a huge tech co and make a dent because huge tech co already has hundreds of engineers on that job full time. Just look at the papers coming out of huge tech co's. They will publish if their technique saves them 0.2%. That's how tight things are already wired in the big leagues. The real money is in huge non-tech co, or medium tech co. You could walk into a goldman sachs type of operation and point…

If you make a dent on server bills on the order of several million dollars a year generally it's not too difficult to make a case that you should be allowed to continue.

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#157
post #35

surprised to see swift 4 times as slow as non-optimized go. Anyone has an explanation ? I know the swift string type is really complex, but i always assumed it was at least performing well..

In case you miss it: https://news.ycombinator.com/item?id=32220587

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#158

Earlier quoted context omitted.

I thought he wrote it in his own language, WEB.

WEB is not a totally different programming language, it is a way to write programs. It is a hybrid (in the form presented in that article, though later versions were centered on other languages or language agnostic) of TeX and Pascal. The code that was written was proper Pascal, the documentation was proper TeX, and there was extra syntax for WEB use in naming and referencing code definitions. The WEB system had two…

Interesting!

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#159
post #75

Earlier quoted context omitted.

You can't walk into a huge tech co and make a dent because huge tech co already has hundreds of engineers on that job full time. Just look at the papers coming out of huge tech co's. They will publish if their technique saves them 0.2%. That's how tight things are already wired in the big leagues. The real money is in huge non-tech co, or medium tech co. You could walk into a goldman sachs type of operation and point…

If you make a dent on server bills on the order of several million dollars a year generally it's not too difficult to make a case that you should be allowed to continue.

[deleted]

Re: Performance comparison: counting words in Python, C/C++, Awk, Rust, and more

#160

Re: the Rust optimized implementation, I was able to get ~20-25% better performance by rewriting the for loops as iterators, using Rust's byte range pattern matching, and a buffered writer, which seems crazy, but it's true. I chalked it up to some crazy ILP/SIMD tricks the compiler is doing. I even submitted a PR[0], but Ben decided he was tired of maintaining and decided to archive the project (which fair enough!).…

In case anyone is interested, I did an optimized, but much more simple, Rust implementation just today[0], which is faster than the optimized implementation on my machine. No indexing into arrays of bytes, no unsafe, etc., no "code golf" measures. Of course, credit where it's due -- these are simply tweaks to Andrew Gallant's code.

Looks like idiomatic Rust, which I think is interesting. Shows there is more than one way to skin a cat.

[0]: https://github.com/kimono-koans/countwords/blob/master/rust/...

Post reply on HN