Hi Randy,

Thanks for an excellent example. It is probably the anonymous function 
combined with some type inference happening. Shall I define a separate 
concatenating function and pass it to map() ?

Cheers,
Jan 

Dňa streda, 21. októbra 2015 15:10:16 UTC+2 Randy Zwitch napísal(-a):
>
> I'm going to guess either it's because of the anonymous function 
> definition within your map function. Note that you aren't getting *exactly* 
> the same results, as the Array types are different.
>
> It also isn't universally true that comprehensions are faster. See the 
> following counter-example:
>
> *julia> **s = ["as", "sdf", "qwer"]*
>
> *3-element Array{ASCIIString,1}:*
>
> * "as"  *
>
> * "sdf" *
>
> * "qwer"*
>
>
> *julia> **@time [length(i) for i in s]*
>
>   0.000019 seconds (10 allocations: 384 bytes)
>
> *3-element Array{Any,1}:*
>
> * 2*
>
> * 3*
>
> * 4*
>
>
> *julia> **@time map(length, s)*
>
>   0.000015 seconds (5 allocations: 256 bytes)
>
> *3-element Array{Int64,1}:*
>
> * 2*
>
> * 3*
>
> * 4*
>
>
> In this case (after multiple runs), map is even or just slightly faster 
> than a comprehension. I presume map is faster here because it knows the 
> result of function will be Array{Int64}, where as the comprehension Julia 
> doesn't know and returns an Array{Any}
>
> On Wednesday, October 21, 2015 at 8:55:50 AM UTC-4, Ján Dolinský wrote:
>>
>> 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