def square(vector)
vector.collect { |i| i**2 }
end def square(vector)
vector.collect { |i| i**2 }
end (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-05sYes. 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.
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.
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.
def square(vector):
return [v**2 for v in vector] 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. 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) 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).
//array comprehension
let squareVec vector = [| for x in vector -> x ** 2.0 |]
squareVec [| 1.0; 2.0; 3.0 |]
[| 1.0; 2.0; 3.0 |] |> squareVec
//inline function
[| 1;2;3 |] |> Array.map (fun x -> x * x)
//extract function
let square x = x * x
[| 1;2;3 |] |> Array.map square
//partial
let squareVec3 = Array.map square
[| 1;2;3 |] |> squareVec3
//partial
let pow x y = y ** x
[| 1.0; 2.0; 3.0 |] |> Array.map (pow 2.0)
//parallel
[| 1.0; 2.0; 3.0 |] |> Array.Parallel.map (pow 2.0)
//can use SIMD on mono (next version on .NET too)
//no first hand experience, but can also use GPU from F#