Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaskoz/9518911 to your computer and use it in GitHub Desktop.
Save vaskoz/9518911 to your computer and use it in GitHub Desktop.
Java vs Go built-in sort speed. Testing 1 BILLION integers.
package main
import "sort"
import "fmt"
import "time"
func main() {
size := 1000000000 // 1 BILLION INTS
a := make([]int, size, size)
for i, _ := range a {
a[i] = i
}
// Sort ascending
start := time.Now()
sort.Ints(a)
fmt.Println("Sorting took (milliseconds): ", float32(time.Since(start).Nanoseconds()/1E6))
// Check that sorted ascending
for i := 0; i < len(a) - 1; i++ {
if a[i] > a[i+1] {
fmt.Println("ERROR! not sorted properly");
}
}
fmt.Println("Success!");
}
import java.util.*;
public class AscendingSortSpeed {
public static void main(String[] args) {
int[] data = new int[1_000_000_000]; // 1 BILLION INTS
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
// Sort to ascending
long start = System.currentTimeMillis();
Arrays.sort(data);
System.out.println("Sort time took (milliseconds): " + (System.currentTimeMillis() - start));
// Verify was sorted correctly
for (int i = 0; i < data.length - 1; i++) {
if (data[i] > data[i+1]) {
System.err.println("FAIL did not sort!!!");
System.exit(1);
}
}
System.out.println("Success!");
}
}
package main
import "sort"
import "fmt"
import "time"
func main() {
size := 1000000000 // 1 BILLION INTS
a := make([]int, size, size)
// Populate array with descending order ints
for i, _ := range a {
a[i] = size - i;
}
// Sort ascending
start := time.Now()
sort.Ints(a)
fmt.Println("Sorting took (milliseconds): ", float32(time.Since(start).Nanoseconds()/1E6))
// Check that sorted ascending
for i := 0; i < len(a) - 1; i++ {
if a[i] > a[i+1] {
fmt.Println("ERROR! not sorted properly");
}
}
fmt.Println("Success!");
}
import java.util.*;
public class DescendingSortSpeed {
public static void main(String[] args) {
int[] data = new int[1_000_000_000]; // 1 BILLION INTS
// Populate with Descending order integers
for (int i = 0; i < data.length; i++) {
data[i] = data.length - i; // Descending order
}
// Sort to ascending
long start = System.currentTimeMillis();
Arrays.sort(data);
System.out.println("Sort time took (milliseconds): " + (System.currentTimeMillis() - start));
// Verify was sorted correctly
for (int i = 0; i < data.length - 1; i++) {
if (data[i] > data[i+1]) {
System.err.println("FAIL did not sort!!!");
System.exit(1);
}
}
System.out.println("Success!");
}
}
package main
import "sort"
import "fmt"
import "time"
import "math/rand"
func main() {
size := 1000000000 // 1 BILLION INTS
a := make([]int, size, size)
for i, _ := range a {
a[i] = int(rand.Int63())
}
// Sort ascending
start := time.Now()
sort.Ints(a)
fmt.Println("Sorting took (milliseconds): ", float32(time.Since(start).Nanoseconds()/1E6))
// Check that sorted ascending
for i := 0; i < len(a) - 1; i++ {
if a[i] > a[i+1] {
fmt.Println("ERROR! not sorted properly");
}
}
fmt.Println("Success!");
}
import java.util.*;
public class RandomizedSortSpeed {
public static void main(String[] args) {
int[] data = new int[1_000_000_000]; // 1 BILLION INTS
for (int i = 0; i < data.length; i++) {
data[i] = (int)(Math.random() * Integer.MAX_VALUE);
}
// Sort to ascending
long start = System.currentTimeMillis();
Arrays.sort(data);
System.out.println("Sort time took (milliseconds): " + (System.currentTimeMillis() - start));
// Verify was sorted correctly
for (int i = 0; i < data.length - 1; i++) {
if (data[i] > data[i+1]) {
System.err.println("FAIL did not sort!!!");
System.exit(1);
}
}
System.out.println("Success!");
}
}
java -Xms6g -Xmx6g DescendingSortSpeed
Sort time took (milliseconds): 1680
./descending_sort_speed
Sorting took (milliseconds): 185148
If data is already sorted in descending order, Java is 110 times faster than Golang
java -Xms7g -Xmx7g RandomizedSortSpeed
Sort time took (milliseconds): 98621
./randomized_sort_speed
Sorting took (milliseconds): 383519
If the input is randomized, the Java sort is 3.89 times faster.
java -Xms6g -Xmx6g SortSpeed
Sort time took (milliseconds): 923
./sort_speed
Sorting took (milliseconds): 142557
If the input is already sorted in ascending order, Java is 154 times faster than Golang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment