On Saturday, January 25, 2014 8:41:08 PM UTC+1, Johan Sigfrids wrote:
>
> On Saturday, January 25, 2014 9:24:17 PM UTC+2, Jason Merrill wrote: 
>>
>>   mma> data = RandomReal[1.,1*^7]; min = .2; max = .3;
>>   mma> Total@Unitize@Clip[data,{min,max},{0,0}]
>>
>> I claim that it takes *a lot* of experience to know that that is the code 
>> that is going to be fast, compared to the other 15 approaches people bring 
>> up there, because the mapping between Mathematica's core constructs and the 
>> machine's core constructs is far more complicated and indirect than for 
>> Julia.
>>
>> In contrast, it doesn't take deep familiarity with Julia's standard 
>> library to come up with about the fastest way to write this operation.
>>
>>   julia> function count_range(arr, min, max)
>>               count = 0
>>               for elt in arr
>>                 if min < elt < max count += 1 end
>>               end
>>               count
>>             end
>>
>> It takes more than one line to write, but it's *obvious*. My Mathematica 
>> license is expired these days, so I can't run the benchmark, but I bet it 
>> also smokes the fast solution in Mathematica.
>>
>
> Having a Mathematica license and being curious I compared them. The Julia 
> version is roughly 10x faster than the Mathematica version.  
>

I compared with Mathematica 9. Total@Unitize@Clip[data,{min,max},{0,0}] was 
for me roughly 3x slower than the Julia version; which sounds reasonable 
since Total, Unitize and Clip run once though the data. In Mathematica 9 
you also have the option to compile to C, which then runs as fast as the 
Julia version:

$CompilationTarget = "C";
f = Compile[{{x, _Real, 1}, {min, _Real}, {max, _Real}}, 
  Module[{count = 0},
   Do[If[min <= x[[i]] <= max, count++], {i, Length@x}]; count], 
  "RuntimeOptions" -> "Speed"];

Reply via email to