Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working through some Lisp exercises using Clojure. If I were to convert Lisp lists to Clojure vectors, solving some of the problems would be simpler, so here is my question:

Does using vec or vector cost a lot in terms of time and/or processing? Does using either function cause a meta state change, or are the values converted and moved to a vector?

share|improve this question
1  
For the most part while doing those exercises, you probably don't want to be using vec or vector but should instead prefer the generic seq operations. vec and vector actually build a vector which costs time and space. For example problem 1 asks you to write the last function. Since the core last function only uses the seq operations I can quickly do (last (range 10000000)) on my machine but doing (last (vec (range 10000000))) waits a minute and then gives me an OutOfMemoryError –  WuHoUnited Oct 7 '12 at 3:48
    
Thanks. You answered the core of my question. A new vector is created. I am also avoiding vec and vector and working with the sequence operators as you suggest. –  octopusgrabbus Oct 7 '12 at 14:35

2 Answers 2

up vote 0 down vote accepted

Both functions return a new vector for you.

vec expect a coll parameter that will be converted to a vector

vector expect args to create a new vector.

Bellow the excecution time for each one:

vec

user=> (time (vec '(1 2 4)))
;= "Elapsed time: 0.043 msecs"
;= [1 2 4]

vector

user=> (time (vector 1 2 3)))
;= "Elapsed time: 0.025 msecs"
;= [1 2 3]
share|improve this answer
2  
-1: timing such short things will not yield reliable results –  naiad Dec 23 '12 at 0:09

I am using criterium library to measure performance. I'm seeing better performance for vec than for apply vector

user=> (use 'criterium.core)
nil
user=> (def mylist (range 100))
#'user/mylist
user=> (bench (apply vector mylist))
WARNING: Final GC required 4.142753917114324 % of runtime
Evaluation count : 8356800 in 60 samples of 139280 calls.
             Execution time mean : 7.271515 µs
    Execution time std-deviation : 112.366680 ns
   Execution time lower quantile : 7.066423 µs ( 2.5%)
   Execution time upper quantile : 7.485808 µs (97.5%)
                   Overhead used : 11.772233 ns
nil
user=> (bench (vec mylist))
Evaluation count : 8949480 in 60 samples of 149158 calls.
             Execution time mean : 6.647705 µs
    Execution time std-deviation : 83.691653 ns
   Execution time lower quantile : 6.473123 µs ( 2.5%)
   Execution time upper quantile : 6.796003 µs (97.5%)
                   Overhead used : 11.772233 ns
nil
share|improve this answer
1  
Programmers is tour conceptual questions answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler –  gnat Dec 21 '13 at 10:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.