Hi,

I'd like to check which approach is a better one for the example below. The 
task is rather simple, each string in vector "s" should be "surrounded" by 
square brackets.

e.g.

s = AbstractString["as", "sdf", "qwer"] # s is typically a lot longer

@time st1 = AbstractString[ "[" * i * "]" for i in s ]
  0.000057 seconds (16 allocations: 672 bytes)
3-element Array{AbstractString,1}:
 "[as]"  
 "[sdf]" 
 "[qwer]"

@time st2 = map(x->"["*x*"]", s)
  0.002932 seconds (33 allocations: 2.035 KB)
3-element Array{ASCIIString,1}:
 "[as]"  
 "[sdf]" 
 "[qwer]"



The both expressions yield same results. I wonder whether from the language 
design point of view one approach should be preferred over the other. 
Comprehension here is considerably faster, I wonder why.

Thanks,
Jan

Reply via email to