Skip to content

Instantly share code, notes, and snippets.

@morisil
Created January 23, 2021 13:21
Show Gist options
  • Save morisil/7447996722f579a5f3370a46265283ec to your computer and use it in GitHub Desktop.
Save morisil/7447996722f579a5f3370a46265283ec to your computer and use it in GitHub Desktop.
package com.xemantic.test.howfast;
import java.util.Arrays;
public class Java2TimesFasterThanC {
private static final int MAX_PAYLOAD_SIZE = 10000;
private static final int INITIAL_NODE_COUNT = 1000;
private static final long MUTATION_COUNT = 10000000L;
private static final int MAX_MUTATION_SIZE = 10;
private static class Node {
private Node previous;
private Node next;
private long id;
private byte[] payload;
private Node(long id) {
this.id = id;
payload = new byte[
(int) (almostPseudoRandom(id) * (double) MAX_PAYLOAD_SIZE)
];
Arrays.fill(payload, (byte) id);
}
void join(Node node) {
previous = node;
next = node;
node.previous = this;
node.next = this;
}
void delete() {
next.previous = previous;
previous.next = next;
// free memory (which is explicit in C and implicit in Java)
}
void insert(Node node) {
node.next = next;
node.previous = this;
next.previous = node;
next = node;
}
}
private static double almostPseudoRandom(long ordinal) {
return (Math.sin(((double) ordinal) * 100000.0) + 1.0) % 1.0;
}
public static void main(String[] args) {
long nodeId = 0;
long mutationSeq = 0;
Node head = new Node(nodeId++);
head.join(new Node(nodeId++));
for (int i = 2; i < INITIAL_NODE_COUNT; i++) {
head.insert(new Node(nodeId++));
}
long nodeCount = INITIAL_NODE_COUNT;
for (long i = 0; i < MUTATION_COUNT; i++) {
int deleteCount = (int) (almostPseudoRandom(mutationSeq++) * (double) MAX_MUTATION_SIZE);
if (deleteCount > (nodeCount - 2)) {
deleteCount = (int) nodeCount - 2;
}
for (int j = 0; j < deleteCount; j++) {
Node toDelete = head;
head = head.previous;
toDelete.delete();
}
nodeCount -= deleteCount;
int insertCount = (int) (almostPseudoRandom(mutationSeq++) * (double) MAX_MUTATION_SIZE);
for (int j = 0; j < deleteCount; j++) {
head.insert(new Node(nodeId++));
head = head.next;
}
nodeCount += insertCount;
}
long checksum = 0;
Node traveler = head;
do {
checksum += traveler.id + traveler.payload.length;
if (traveler.payload.length > 0) {
checksum += traveler.payload[0];
}
} while (
(traveler = traveler.next) != head
);
System.out.println("node count: " + nodeCount);
System.out.println("checksum: " + checksum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment