3 ms·
Python is interpreted, C and C++ are compiled. They target different niches. Python is more focused on ease of use than performance. Usually when writing scien
by messe 9y ago
Python is interpreted, C and C++ are compiled. They target different niches. Python is more focused on ease of use than performance.
Usually when writing scientific code in Python you're going to want to at the very least use numpy. It wraps various C functions for many time consuming tasks.
To get the most out of numpy you're going to have to vectorize you're code, as python's looping constructs are notoriously slow. Instead of writing something like
for i in range(0, len(v)):
v[i] = w[i]**2 + w[i]
You'd write something more along the lines of
v[:] = w**2 + w
- pmoriarty 9y agoIt is up to the implementation of a language whether that language is compiled or interpreted. For instance, there are C interpreters and Python compilers. Neither language is limited to being one or the other. It's just that usually people use C compilers and Python interpreters, but there's nothing inherent in those languages that limits them to those. It's all up to the implementation.
- kibwen 9y agoThis is true, but not always in a useful sense. There exist language features whose semantics are expressly dynamic, and if your language includes those features it will frustrate static compilation. For example, if your language includes `eval` (as Python does), then any precompiled binary would also need to include a complete Python interpreter. This is why efforts to compile Python (and other languages that are traditionally interpreted) often omit such features, and why languages that intend to be compiled tend to forgo them in the first place.
- pjmlp 9y agoWhich is why for me the best way is to be able to choose between JIT/AOT deployments, instead of a plain interpreter. Maybe one day PyPy will finally become the default option.
- messe 9y ago> It is up to the implementation of a language whether that language is compiled or interpreted. While that's perfectly true, it didn't seem necessary to go into it.
- no_wizard 9y agoThis seems like it would be great for a list comprehension. I believe the main appeal to using comprehensions is that it speeds up the iteration process at run time so wouldn't a list comprehension be just as useful here if not more so then?. You could also use the functools and itertools library for really fast iterations, possibly, depending on what it is you are trying to achieve. If you are specifically talking lists (arrays) why not use Deque? It is set matched and python saves run time by automatically knowing each value will be a finite forward sequence
- innocenat 9y agoBut would that be faster than numpy? numpy in typical setup would use SIMD or other math kernel (mkl, blas, etc).
- lima 9y agoIt wouldn't. Pure Python code is never going to out-perform a low-level optimized C library. Especially not when comparing iterative vs vectorized code.
- no_wizard 9y agoOh no of course not. Sorry I must have missed something here. I was speaking specifically of iterating over/with values. Numpy is optimized for this kind of thing. Though you can help yourself out and make sure you python code is also optimized by using comprehensions and itertools I would suppose