5 ms·
def square(vector) vector.collect { |i| i**2 } end
by heliumcraft 13y ago
def square(vector)
vector.collect { |i| i**2 }
end
- adamnemecek 13y agoCool, it will also be probably 30x slower.
- ICWiener 13y agoDoes speed really matter? I still prefer the readable Lisp version (SBCL, no optimization): (defun square-vect (vect) (map 'vector (lambda (x) (* x x)) vect)) (setq *vector* (map 'vector (lambda (x) (random 32767)) (make-array 10000 :element-type 'integer))) (time (dotimes (i 1000) (square-vect *vector*))) Results : 0.084 seconds of real time 0.084005 seconds of total run time (0.084005 user, 0.000000 system) 100.00% CPU 218,686,836 processor cycles 79,876,912 bytes consed On my system, the C++ version perform like this: ./main 1 1: 270756377 - elapsed time: 0.022324s ~$ ./main 2 2: 270756377 - elapsed time: 0.018883s ~$ ./main 3 3: 270756377 - elapsed time: 0.016929s ~$ ./main 4 4: 270756377 - elapsed time: 0.014268s ~$ ./main 5 5: 270756377 - elapsed time: 0.016093s ~$ ./main 6 6: 270756377 - elapsed time: 0.018651s ~$ ./main 7 # :-) 7: -1455582645 - elapsed time: 5.3e-05s
- crusso 13y agoDoes speed really matter? Yes. C++ is chosen by developers who tend to choose performance over elegance. When I was doing a lot more C++, I was intimately aware of the deep performance implications of choosing one data type and implementation technique over another.
- ICWiener 13y agoHonestly, I understand; I am really bothered too when I see people applying techniques without even thinking about performance implications (and btw, this is also true for security, correctness, portability ... implications). C++ is viewed as the ultimate language when it comes to performance, for good reasons; of course, if I ever need a fast program, I will consider it. But as an exercise, why not try to optimize the CL code, first? By using type declarations and avoiding allocating memory, here is an optimized CL version that runs under 0.01s, beating the C++ code (note that the original C++ code could be optimized too; e.g. why check the command-line argument every time?). (defpackage :test (:use :cl)) (in-package :test) (defconstant +vector+ (map '(simple-array fixnum (10000)) (lambda (x) (declare (ignorable x)) (random 32767)) (make-array 10000 :element-type 'fixnum))) (let ((result (make-array 10000 :element-type 'fixnum))) (declare (type (simple-array fixnum (10000)) result)) (flet ((square (x) (declare (optimize (speed 3) (safety 0) (debug 0)) (fixnum x)) (the fixnum (* x x)))) (defun square-vect () (declare (optimize (speed 3) (safety 0) (debug 0)) (type (simple-array fixnum (10000)) +vector+)) (loop for i from 0 to 9999 do (setf (aref result i) (square (aref +vector+ i)))) result))) (time (dotimes (i 1000) (square-vect))) Evaluation took: 0.009 seconds of real time 0.008000 seconds of total run time (0.008000 user, 0.000000 system) 88.89% CPU 22,650,364 processor cycles 0 bytes consed Evaluation took: 0.009 seconds of real time 0.008000 seconds of total run time (0.008000 user, 0.000000 system) 88.89% CPU 22,789,708 processor cycles 0 bytes consed Evaluation took: 0.009 seconds of real time 0.008001 seconds of total run time (0.008001 user, 0.000000 system) 88.89% CPU 22,418,566 processor cycles 0 bytes consed Evaluation took: 0.009 seconds of real time 0.012001 seconds of total run time (0.012001 user, 0.000000 system) 133.33% CPU 22,498,018 processor cycles 0 bytes consed EDIT: of course, now, you can say that the programs is unsafe under certain conditions; you may have an issue with the "defconstant" declaration (replacing it with a variable leads to 0.012s results). But after all, there is no specification. In practice, one must deal with actual requirements and sensible measurments.
- GFK_of_xmaspast 13y agoSpeed always matters. (Sometimes this is speed of development/iteration, sometimes this is speed of execution.)
- heliumcraft 13y agoLikely not that much slower. I rather have slightly slower code that is easy to develop & maintain, than code that is fast but hard to read, hard to maintain and easy to introduce bugs in to.
- zanny 13y agoI write a lot of C++, and nothing about a standard algorithm loop is hard to read or maintain. For the Python or C++ guru both are trivial to work with. If I were doing fancy conditional transforms on maps with non-trivial modifications (ie, string manipulation) I'd almost always try to use Python first, though.
- spoiler 13y agoY not map.
- ptr 13y agocollect and map are the same thing, just different names. IIRC, collect was the name used in Smalltalk for map operations.
- milliams 13y agoI like the Python version of: def square(vector): return [v**2 for v in vector]
- ptr 13y agoHaskell: let square = map (^ 2) Real partial application and currying is something I'm longing for in other languages; it's easy to fall for the beauty of the Point Free Style.
- icebraining 13y agoWell, in Python that would be: from functools import partial square = partial(map, lambda i: i**2) EDIT: I guess it's still not the same, since the lambda isn't a partial application. But I don't think we can just set the exponent in pow(), we need to create a function that takes the arguments in the reverse order. Maybe: from functools import wraps, partial def flip(func): 'Create a new function from the original with the arguments reversed' @wraps(func) def newfunc(*args): return func(*args[::-1]) return newfunc _pow = flip(pow) #Take Y, then X # Then the equivalent would be square = partial(map, partial(_pow, 2) Of course, flip is rather long-winded because it's a generic function, one could just do: _pow = lambda y, x: pow(x, y)
- deleted 13y ago[deleted]
- cousin_it 13y agoOr SQL: SELECT x*x FROM vector; There's just so many things that SQL got right: - Non-programmers actually read and modify SQL code. I swear, it happens. It doesn't happen very much even with Python, and it will never happen with C++. - It can join multiple data structures in a single query, on arbitrary criteria. - A query can be optimized as a whole. Optimizers freely reorder the parts and actually come up with good plans. - The presence or absence of indexes (data structures used for acceleration) is transparent to the query. Can any other language do that? A lot of the code I've seen would be 10x to 100x smaller if expressed in SQL, or an SQL-like language that can handle nested data structures (which is something that I really want).