> You can express in Python some things in a single line, where you need hundreds of lines in C++ or other languages.
citation needed.
Re:Why Python is Slow: Looking Under the Hood
by Borogove on Friday May 09, 2014 @07:51PM
(#7723904)
Example:
x = {'foo':34,'bar':[1,2,3],'baz':'quux'}
The headers alone for STL maps are hundreds of LOC.
Counting headers is rather unfair.
C++11:
map<int, char> x = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
Any not-ancient C++ using boost Assign:
map<int, char> x = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');
Shamelessly stolen from here:
http://stackoverflow.com/questions/138600/initializing-a-sta...
That's tricky, because C++ is statically typed and you've selected a varying value type, but here's the closest thing in real terms.
#include <map>
#include <vector>
#include <string>
#include <boost/any.hpp>
using boost::any;
using std::map;
using std::string;
using std::vector;
int main() {
map<string,any> x = {{"foo", 34}, {"bar", vector<int>{1,2,3}}, {"bar", "quux"}};
}
Syntax overhead amortizes to constant factor ;)
Re:Why Python is Slow: Looking Under the Hood
by GFK_of_xmaspast on Friday May 09, 2014 @08:59PM
(#7724076)
I agree with the upstream comment about expressiveness, but this isn't a fair example.
Re:Why Python is Slow: Looking Under the Hood
by PythonicAlpha on Friday May 09, 2014 @05:32PM
(#7723362)
I have none, it is my personal experience, since I programmed in C++ (and other languages) before. So cite me, if necessary.
And if you really need to have a piece of your code execute really quickly, you can just make C extension that python can easily invoke.
Python's support for this is awesome, but there is a gap: you lost cross platform. You now have to figure out how to make it compile on every platform that anybody could every possibly want to run it on, including ones not yet invented in the future ... Jython / Java (or other JVM languages) are quite a nice option in that space.
Re:Why Python is Slow: Looking Under the Hood
by PythonicAlpha on Saturday May 10, 2014 @09:56AM
(#7725393)
Of course you loose a little platform independence, but that is something you don't have anyway when you program in C++ or that kind. Also you get rather rich support for makefiles that are cross platform. I had no problems till now with C code I wrote that integrates into the Python environment. When you don't need OS specifics and have knowledge how to implement things CPU independent (given a common >=32bit architecture) in C, you will have little to no trouble at all.
Also one little hint: Use Cython for enhancements. Cython is great for implementing small enhancements when you don't want to hassle with refcounts and the normal Python API. Also your enhancements are automatically compatible to Python 2 + Python 3. You can also call C functions from Cython very easily for full speed. Cython is really great, when you don't need to optimize to the last nanosecond.
The few percent of running speed that you might loose, are neglect-able in most cases against the win in development speed.
In many applications you don't need the full CPU power, but often times you are hindered by e.g. the disk speed or other factors ... and than you don't lose anything when you are a little bit slower in some minor tasks.