Tell me more ×
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 Lips 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

1 Answer

up vote 2 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
-1: timing such short things will not yield reliable results – sparkleshy Dec 23 '12 at 0:09

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.