7 ms·
Yes. It's much faster. The reason C++ is fast is because you have explicit control over allocations and it compiles to native code (among other reasons). There
by overgard 11y ago
Yes. It's much faster. The reason C++ is fast is because you have explicit control over allocations and it compiles to native code (among other reasons). There are also a lot of subtle things that you can control in C++ that you can't in python, like memory layout. Memory layout is extremely important for CPU cache, and CPU cache is extremely important for performance. (Reading data from RAM is generally orders of a magnitude slower than reading from an L1 or L2 cache. For some reason this never gets mentioned in university CS courses, but there are a lot of data structures that are "theoretically" fast for certain things but in practice very unfriendly to hardware. For instance, linked lists. Sure insert/delete is O(1), but since linked lists are quite possibly the least cache friendly data structures in existence, you're better off with a vector/array even if you're inserting all the time. I'm not just making this up, you can read talks by people like Bjarne Stroustroup on this topic)
As to whether it matters for the algorithm, it depends on the algorithm. It's almost always going to be much faster in C, but, how fast do you really need it to be? Python is very good at saving programmer time, so if performance is not particularly important, I just wouldn't worry about it.
- mrob 11y agoMike Acton gave a great presentation on this kind of optimization at CppCon2014: https://www.youtube.com/watch?v=rX0ItVEVjHc https://www.youtube.com/watch?v=rX0ItVEVjHc
- CyberDildonics 11y agoWhile this is true, and linked lists are somewhere between exotic and obsolete, the presentation by Bjarne Stroustroup had buried in it him saying 'the time to find the element takes so long a vector is always faster'. But, of course if you include a search linked lists are going to be way slower. Also I wouldn't say that the same algorithm would be faster in C, there is no reason why C++ can't be as fast as C, even idiomatic modern C++.
- floatboth 11y agoExotic? Obsolete? Really? Linked lists are used in pretty much all functional programming languages (because head/tail splitting is extremely useful)
- CyberDildonics 11y agoExotic at least for performance. Not only do you have to chase pointers to loop through them, you have to deal with the heap on every append or delete. They can be great for software that doesn't have speed as a high priority.
- woodman 11y agoI'd be interested to know how you arrived at this conclusion, like what domain do you spend all your time in where this is true? I frequently see linked lists used for all manner of things, in some widely deployed performant code: https://github.com/mackyle/sqlite/search?q=linked+list https://github.com/mackyle/sqlite/search?q=linked+list https://github.com/freebsd/freebsd/search?l=c&q=linked+list https://github.com/freebsd/freebsd/search?l=c&q=linked+list
- malisper 11y agoStroustrup gave a talk where he demonstrated that the performance of a linked list is terrible compared to a vector. Here[0] is a summary. [0] http://bulldozer00.com/2012/02/09/vectors-and-lists/ http://bulldozer00.com/2012/02/09/vectors-and-lists/
- woodman 11y agoI'm not debating that iterating over a vector is faster than iterating over a linked list, I'm asking where this guy has seen linked lists become "exotic". I'd love to dive into that code, because I've actually spent a lot of time implementing parallel arrays vs linked lists or arrays of structs, it would be nice to see how folks have addressed the pain points like sorting in constant space...
- uxcn 11y agoC is absolutely not faster than C++. Generic types in C are typically done through type erasure or an intrusive type. C++ templates explictly allow different instances of code to be generated for each of the types. So, the code can be specialized for each type, which most of the time is faster. In general, template metaprogramming allows for drastically more complex optimization at compile time. The biggest reason to write in C over C++ is simplicity and higher level of control over algorithms and data structures. Portability is another reason though.
- vardump 11y agoVery much right, unless CPU icache limits start to bite. C++ templates can generate a lot of code. For simple functions, templates are usually always a pure win. For example, C qsort is horrible. Function pointer to a otherwise so inlinable small sort function is always going to get beaten by C++ template based sort. But if each iteration of loop needs to fetch 10 kB of instructions because of specialized separately compiled templated forms, suddenly pointers win again. It can also invalidate TLB, branch predictor, etc. caches. But most cases templates are a win. Just remember microbenchmarking can really mislead here, if the program as whole has an icache bottleneck as a consequence.
- cactusface 11y agoYeah, but nothing says you can't specialize your C code.
- uxcn 11y agoAnd C compilers can generate specializations if everything is in the same translation unit. What I was trying to point out is that less abstract doesn't imply better performance. It isn't C > C++ > Python. What the machine ultimately has to do is what matters.
- cactusface 11y agoI think programs written in dynamic languages are generally harder to optimize because they're more likely to take advantage of complex features. Since these languages often compile to C, you can obviously write the same program in C, but you aren't as likely to.
- overgard 11y agoI tend to use C and C++ interchangeably (and yeah, I know there are vast differences), I didn't mean to imply that you can't write code in C++ that is as fast as C, I just get bored writing "C/C++"
- petulla 11y agoThis is not always true but depends on the operation. Cython/Numpy take advantage of Python's extensibility at the operating system level. See: http://stackoverflow.com/questions/2119761/simple-python-challenge-fastest-bitwise-xor-on-data-buffers/2566106?benchmark#2566106 http://stackoverflow.com/questions/2119761/simple-python-cha...
- deleted 11y ago[deleted]
- sebastianconcpt 11y agoI wonder why we don't yet have CPUs and memory access that are more friendly to higher level structures. The Burroughs 5000 tried to do that and it worked. http://www.smecc.org/The%20Architecture%20%20of%20the%20Burroughs%20B-5000.htm http://www.smecc.org/The%20Architecture%20%20of%20the%20Burr...