• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Comparing Arrays.sort() and Collections.sort()

 
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Time difference between Arrays.sort() and Collections.sort( ) is enormous, why?



size 1000000
array sort time 12.797650 ms
array sort time/size/log2(size) 0.642079 ns
list sort time 461.183508 ms
list sort time/size/log2(size) 23.138345 ns
arrays speedup = 36.036578 times

size 2000000
array sort time 26.375102 ms
array sort time/size/log2(size) 0.630032 ns
list sort time 857.165750 ms
list sort time/size/log2(size) 20.475430 ns
arrays speedup = 32.499049 times

size 5000000
array sort time 74.852897 ms
array sort time/size/log2(size) 0.672729 ns
list sort time 2582.375501 ms
list sort time/size/log2(size) 23.208717 ns
arrays speedup = 34.499339 times

 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're comparing apples and oranges. There's already quite some overhead when using Integer instead of int because of auto-unboxing. If I change the int[] into an Integer[] the array speedup is around 1, sometimes even less than 1 meaning they have equivalent performances.

Before Java 8 the list sorting would have been worse though, because Collections.sort always called list.toArray() first. It now delegates to list.sort(null) which by default calls this.toArray() but ArrayList has overridden it to directly delegate to Arrays.sort.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic