# What does using (vec col) or (vector arg1 & args) cost?

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?

-
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

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]
``````
-
-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%)