Simon,
Thank you for the  great reply.

I checked with your fastest version against Java. Here are the timings 
(average of five trials for each size of array). 

size af array, javatime, juliatime
1000, 0.005, 0.001
2000, 0.003, 0.003
4000, 0.012, 0.016
8000, 0.046, 0.062
16000,0.210, 0.250
32000, 0.905, 1.00
64000, 3.745, 3.991

just for the record

ravi@hod:~/projects/algorithmsbenchmarks/julia/src$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

ravi@hod:~/projects/algorithmsbenchmarks/julia/src$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |              Version 0.4.0-dev+6563 (2015-08-10 
12:14 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 87a956b (19 days old master)
|__/                   |  x86_64-linux-gnu

Thanks again, your answer has been *extremely* useful




On Saturday, August 29, 2015 at 10:18:19 PM UTC+5:30, Simon Danisch wrote:
>
> Here are some code snippets, which apply the knowledge from the performance 
> tip section 
> <http://julia.readthedocs.org/en/latest/manual/performance-tips/>
>
> https://gist.github.com/SimonDanisch/a36dcc824034c43b5d78#file-selectionsort-jl
> Spoiler: the fastest version is 15 times faster.
> The one with boundchecks is 8-10 times faster, which is on eye level with 
> java.
> That's not a big surprise, since Julia and Java are often equally fast, 
> but you would need to use the unsafe collection to get rid of boundchecks 
> in Java, from what I know.
>
> Am Samstag, 29. August 2015 18:20:05 UTC+2 schrieb Ravi Mohan:
>>
>> So I wrote an inplace selection sort in Julia,  to compare its 
>> performance against a Java counterpart,
>> Here is the code
>>
>> function selectionSort!{T}(v::AbstractVector{T}, f)
>>     
>>     n = length(v)
>>
>>     for i=1:n-1
>>         min  = i
>>         for j=i+1:n
>>            if f(v[j], v[min])
>>                  min = j
>>            end
>>         end
>>
>>         tmp = v[i]
>>         v[i] = v[min]
>>         v[min] = tmp
>>     end
>>     
>> end # selection Sort
>>
>> The equivalent java code is 
>>
>>      public static void sort(Comparable[] a) {
>>         int N = a.length;
>>         for (int i = 0; i < N; i++) {
>>             int min = i;
>>             for (int j = i + 1; j < N; j++) {
>>                 if (a[j].compareTo(a[min]) < 0) {
>>                     min = j;
>>                 }
>>             }
>>             Comparable temp = a[i];
>>             a[i] = a[min];
>>             a[j] = temp;
>>         }
>>     }
>>
>> I have the following questions
>> (1) the function f passed in to selectionSort should have the type f: 
>> (T,T) => Boolean, ie it takes two elements of the Vector and tells whether 
>> they are in correct order or not. This could be <  for example. How do I 
>> state this in Julia iow, how do I specify the function signature?
>>
>> (2) Are there any horrendous inefficiencies in the Julia code which are 
>> apparent to experienced Julians? When I benchmark  against the equivalent 
>> Java I'm getting a factor of 10+ slowdown for larger arrays (though both 
>> seem O(n^2) as it should be)
>>
>> size(of array, uniform distribution of doubles between 0 and 1),time 
>> taken to sort in Java (in milliseconds), time taken to sort in Julia. both 
>> times are an average of 5 runs for each array size
>>
>> 1000,0.003,0.010
>> 2000,0.003,0.041
>> 4000,0.011,0.165
>> 8000,0.044,0.664
>> 16000,0.206,2.660
>>
>>
>> I readily admit I have very little idea of what it takes to write 
>> performant code in Julia. Any help greatly appreciated.
>>
>> Of course, I might be doing something really stupid in the first place, 
>> which happens all to frequently sadly enough. Please point out any mistakes 
>> in my code.
>>
>> Thanks in advance,
>> Ravi
>>
>

Reply via email to