performance measurements

Each table row shows performance measurements for this Java program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0001.261.2751,424894  2% 1% 1% 99%
5,000,0006.336.3495,140894  1% 1% 0% 100%
50,000,00035.9335.96299,028894  1% 1% 0% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read thread-ring benchmark to see what this program should do.

 notes

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)

 thread-ring Java #4 program source code

/**
 * The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Fabien Le Floc'h
 *
 * Java implementation of thread-ring benchmark. Best performance is achieved with 
 * MAX_THREAD=1 as the thread-ring test is bested with only 1 os thread.
 * This implementation shows using a simple thread pool solves the thread context
 * switch issue.
 */

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class threadring {
    private static final int MAX_NODES = 503;
    private static final int MAX_THREADS = 503;

    private ExecutorService executor;
    private final int N;

    public static void main(String[] args) {
        threadring ring = new threadring(Integer.parseInt(args[0]));
        Node node = ring.start(MAX_NODES);
        node.sendMessage(new TokenMessage(1,0));
    }
    
    public threadring(int n) {
        N = n;
    }

    public Node start(int n) {
        Node[] nodes = spawnNodes(n);
        connectNodes(n, nodes);
        return nodes[0];
    }

    private Node[] spawnNodes(int n) {
        executor = Executors.newFixedThreadPool(MAX_THREADS);
        Node[] nodes = new Node[n+1];
        for (int i = 0; i < n ; i++) {
            nodes[i] = new Node(i+1, null);
        }
        return nodes;
    }
    
    public void connectNodes(int n, Node[] nodes) {
        nodes[n] = nodes[0];
        for (int i=0; i<n; i++) {
            nodes[i].connect(nodes[i+1]);
        }
    }

    private static class TokenMessage {
        private int nodeId;
        private volatile int value;
        private boolean isStop;

        public TokenMessage(int nodeId, int value) {
            this.nodeId = nodeId;
            this.value = value;
        }

        public TokenMessage(int nodeId, int value, boolean isStop) {
            this.nodeId = nodeId;
            this.value = value;
            this.isStop = isStop;
        }
    }

    private class Node implements Runnable {
        private int nodeId;
        private Node nextNode;
        private BlockingQueue<TokenMessage> queue = new LinkedBlockingQueue<TokenMessage>();
        private boolean isActive = false;
        private int counter;

        public Node(int id, Node nextNode) {
            this.nodeId = id;
            this.nextNode = nextNode;
            this.counter = 0;
        }

        public void connect(Node node) {
            this.nextNode = node;
            isActive = true;
        }

        public void sendMessage(TokenMessage m) {
            queue.add(m);
            executor.execute(this);
        }


        public void run() {
            if (isActive) {
                try {
                    TokenMessage m = queue.take();
                    if (m.isStop) {
                        int nextValue = m.value+1;
                        if (nextValue == MAX_NODES) {
//                            System.out.println("last one");
                            executor.shutdown();                            
                        } else {
                            m.value = nextValue;
                            nextNode.sendMessage(m);
                        }
                        isActive = false;
//                        System.out.println("ending node "+nodeId);
                    } else {
                        if (m.value == N) {
                            System.out.println(nodeId);
                            nextNode.sendMessage(new TokenMessage(nodeId, 0, true));
                        } else {
                            m.value = m.value + 1;
                            nextNode.sendMessage(m);
                        }
                    }
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }
}

 make, command-line, and program output logs

Wed, 19 Nov 2014 09:10:25 GMT

MAKE:
mv threadring.java-4.java threadring.java
/usr/local/src/jdk1.8.0_25/bin/javac threadring.java
0.73s to complete and log all make actions

COMMAND LINE:
/usr/local/src/jdk1.8.0_25/bin/java  -server -XX:+TieredCompilation -XX:+AggressiveOpts threadring 50000000

PROGRAM OUTPUT:
292

Revised BSD license

  Home   Conclusions   License   Play