5 ms·
Those statements are too broad to be anything other than flamebait. Which specific code did you benchmark? That’s not to say that CPython isn’t slower than a c
by acdha 9y ago
Those statements are too broad to be anything other than flamebait. Which specific code did you benchmark?
That’s not to say that CPython isn’t slower than a compiled language but in common usage the gaps usually end up being much smaller because most of us aren’t paid to run fib() and things like I/O dominate or code ends up being in the same C libraries (libjpeg, OpenSSL, etc.).
I’ve seen multiple cases where Python beat C/Go because the app used something like dictionaries which was heavily optimized C code in the standard library.
Again, compiled languages should usually win but the details matter, especially since most people don’t do the exact same work you do. Simply tossing around big numbers with no support contributes noise rather than insight to the conversation.
- hasenj 9y ago> Which specific code did you benchmark? I had a csv file with ~16k entries. Each entry has a lat, lon, name and id. The task: given a (lat, lon) pair, list all entries within 5km. The simplest straight forward solution is to iterate the list, measure the distance of each item to the given point, and add it to the return list if the distance is within the desired range. No crazy optimization or anything. The results were something roughly like this: The D version runs in 2~4ms when compiled with optimizations turned on to the max, and about 6~8ms without optimizations (just default compiler settings). The Go version runs in about 6~8ms. (The Go compiler has no option to produce optimized code). The python version runs in about 50ms. That's 10x slower than Go and 20x slower than D. This is python 3. Using pypy shows absolutely no difference in performance. Obviously the time to load the data from the csv file into a list is not counted in the above results. > I’ve seen multiple cases where Python beat C/Go because the app used something like dictionaries which was heavily optimized C code in the standard library. That's exactly backwards. The Go and D versions use structs which are compact data structures that pack the data together. So the entire array fits in one contiguous block of memory. Python has no concept of structs, so you have to deal with dictionaries. In general, writing optimized code is not a good idea. You should almost always write the most plain and straight forward code. Because fast languages are fast by default, there's no optimization required. The straight forward solution is fast enough. If you are writing Python, your code will be slow by default. You will need to think about ways to optimize it if you want better performance.
- weberc2 9y agoWorth noting that Python's csv library is optimized C, and it handily beats Go's so this benchmark must be dominated by non-CSV parsing.
- hasenj 9y agoThe csv parsing is not part of the benchmark. The only thing I measured is iterating the list and matching entries within range of the input coordinate.
- maxerickson 9y agoPeople talk about numpy as a numerical library, another way to think of it is as an api for interacting with contiguous chunks of memory.
- deleted 9y ago[deleted]
- tychver 9y agoThe whole Ruby VM only takes ~50ms to start, so that sounds a bit slow. I wrote a quick version in Ruby and it only takes 5ms: https://gist.github.com/jamatthews/d910a2b39c87a871264dd31d13d5db9a https://gist.github.com/jamatthews/d910a2b39c87a871264dd31d1...
- hasenj 9y agoIt's a bit more complicated. I'm measuring geographical distances so I'm using haversine from math import radians, cos, sin, asin, sqrt def distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: # taken from: https://stackoverflow.com/a/4913653/35364 # convert decimal degrees to radians lon1 = radians(lon1) lon2 = radians(lon2) lat1 = radians(lat1) lat2 = radians(lat2) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 c = 2 * asin(sqrt(a)) r = 6371008 # Average radius of earth in meters return c * r
- weberc2 9y ago> Those statements are too broad to be anything other than flamebait. Which specific code did you benchmark? > Simply tossing around big numbers with no support contributes noise rather than insight to the conversation. I resent that you characterize my comment as "flamebait" for lacking supporting evidence for my claim, when I was merely responding to your unsupported claim that Python competes with compiled languages. I chose to be broad because (as you point out), the range varies considerably by application and the margin for error is high, so it would be misleading to be artificially precise. It's perfectly reasonable that you want supporting evidence, but it doesn't make my post "lower quality" than yours. As for "what I benchmarked", my comment was based on a decade of experience with these languages, including translating many proprietary systems from one language to another (sometimes a faster->slower language and sometimes a slower->faster language depending on whether I was optimizing for performance or security or maintainability). > That’s not to say that CPython isn’t slower than a compiled language but in common usage the gaps usually end up being much smaller because most of us aren’t paid to run fib() and things like I/O dominate or code ends up being in the same C libraries (libjpeg, OpenSSL, etc.). > I’ve seen multiple cases where Python beat C/Go because the app used something like dictionaries which was heavily optimized C code in the standard library. I don't dispute that there are cases where Python beats naive C/Go. In particular, Python's csv lib is optimized C code, and it beats Go's CSV parser handily. If all you're doing is parsing CSVs and not actually calling into Python, you should probably stick with Python. While it's true that many applications are I/O bound, my figure was based on CPU time--it wouldn't make sense to judge languages by applications that are inherently I/O bound (although Go's async-by-default is much nicer than Python's async API). Besides, many applications are not I/O bound.
- acdha 9y ago> I resent that you characterize my comment as "flamebait" for lacking supporting evidence for my claim, when I was merely responding to your unsupported claim that Python competes with compiled languages. My point was simply that while it's easy to sit around talking about microbenchmarks that's not helpful for most developers. I've seen this cycle repeat so many times over the last couple decades because we're culturally prone to focusing on very small things and ignoring the larger picture: Most developers are not employed to make a single thing as fast as possible but instead have to ship new features on a regular basis, handle a wide range of inputs correctly, etc. Most of the performance problems people notice are due to application logic rather than low-level language characteristics, and that tends to mean things like I/O (API calls, poor file access patterns, etc.) or inefficient memory access patterns rather than how fast a loop can be. I've seen slow applications in compiled languages replaced with a dynamic language and a big performance win not because of the underlying language characteristics but because writing in a lower-level (C, C++, Go) or more convoluted (Java J2EE) environment took enough longer that the development team was struggling just to complete features and the {Perl,PHP,Python,JS} team had considerably more time to optimize the core design. I've also seen developers in all of those create huge labyrinths which made it hard to reason about the code's performance characteristics, and so my main concern these days is far less caring about the language than the overall amount of complexity you're asking developers to reason about and the related questions about the project and staffing. Given unlimited time and budget, yes, a compiled language will probably win but for most projects the better question is “what tools will allow me to meet my goals with acceptable performance?” and that's a very different question.