5 ms·
Python is typically more like 40x slower than C, not 400x. Passing arrays by reference really just isn't that big of an issue in practice. We tend to write bot
by StefanKarpinski 13y ago
Python is typically more like 40x slower than C, not 400x.
Passing arrays by reference really just isn't that big of an issue in practice. We tend to write both non-mutating and mutating versions of most array functions, distinguished by whether they end in ! or not. Typical development procedure is to write an algorithm using non-mutating APIs and get that right; then you figure out where argument copying is slowing things down (using e.g. the built-in profiler) and modify the code to avoid copying, sometimes by simply adding a ! or sometimes by pre-allocating some work array and using a mutating function on it. Compare this with a similar scenario in Matlab where there simply is no straightforward way to avoid copying – if the JIT is clever enough to avoid copying you're in luck, but often it's not that smart and you're forced to live with the copying or drop into C.
- ScottBurson 13y agoI've never used Matlab and know little about it, but in general, there are much better ways to implement value semantics than by copying entire collections. Clojure's collections probably reflect the state of the art here. You might also find Chris Okasaki's book on functional data types of interest (I think Clojure's sequence type was been borrowed from Okasaki). FSet does it with balanced binary trees, which gives log-time update and log-time indexing, which are acceptable for many applications but not the best you can do for sequences.
- cdavid 13y agoThe problem is that for arrays, most non-trivial datastructures have a high cost because of indirection. Memory is already the main bottleneck in many numerical operations, and adding pointers is often terrible for memory performance. For arrays, even large, reasoning in terms of O(N) for access patterns is useless, because the constants are what matters.
- StefanKarpinski 13y agoRepresenting vectors, matrices or general arrays as anything other than a single contiguous chunk of inline values is a complete non-starter for numerical work since you can't call BLAS, LAPACK, FFTW or any other high quality numerical library on e.g. Clojure's vectors. Without those libraries, you lose many orders of magnitude performance on crucial operations like matrix multiplication.