6 ms·
While I'm more of a pythonist than a C-ist, hearing "1000x speedup" and "line for line" to me implies that you aren't writing idiomatic python. Idiomatic python
by joshuamorton 9y ago
While I'm more of a pythonist than a C-ist, hearing "1000x speedup" and "line for line" to me implies that you aren't writing idiomatic python. Idiomatic python is (often) faster than not, and (often) more difficult to translate to lower level languages.
As a simple example, list-comprehensions are faster than loops, and can't be line for line translated into C++.
- pjmlp 9y agoDepends, I bet they can be easily translated to some LINQ like implementation in C++17. List comprehensions are just syntax sugar for map/filter/fold.
- joshuamorton 9y agoA jaunt through SO didn't give anything reasonable. This is what SO had to offer: https://stackoverflow.com/questions/36339533/how-to-generate-vector-like-list-comprehension/36340883 https://stackoverflow.com/questions/36339533/how-to-generate..., and a quick check of new C++17 features didn't show any that would obviously improve on that. There's nothing implicitly stopping you from doing some macro magic to implement it, but its not there naturally.
- pjmlp 9y agoSO is not the only source of truth. #include "cpplinq.hpp" int computes_a_sum () { using namespace cpplinq; int ints[] = {3,1,4,1,5,9,2,6,5,4}; auto result = from_array (ints) >> where ([](int i) {return i%2 ==0;}) // Keep only even numbers >> sum () // Sum remaining numbers ; return result; } Taken from https://archive.codeplex.com/?p=cpplinq https://archive.codeplex.com/?p=cpplinq Done with C++11. I only referred C++17, because of the lambda improvements done since they got introduced in C++11.
- Sean1708 9y ago> list-comprehensions are faster than loops Are they really though? In my experience you'll gain a couple of percent because you're getting rid of that call to append, but that's hardly the orders of magnitude OP was looking for.
- joshuamorton 9y agoYes, I wasn't saying they would fix this problem (they won't), but making the broader point that in general, more idiomatic/pleasant looking code is faster. This is neither of those, and so my hunch is that there are improvements that can be made to both form and function.