Java: Atomic, volatile, synchronized?

Otro sencillo benchmark para tener un orden de magnitud de que es más rápido:

B (boolean), VB (volatile boolean), AB (AtomicBoolean), Bsync (boolean synchronized)

B	100000000	900ms	100000000req/s	111111req/ms	best (reference)
VB	100000000	2497ms	50000000req/s	40048req/ms	2nd better
AB	100000000	2513ms	50000000req/s	39793req/ms	3rd better
Bsync	100000000	5715ms	20000000req/s	17497req/ms	worse

El codigo de prueba:

import java.util.concurrent.atomic.AtomicBoolean;

public class TestBoolean {
	private static boolean b = true;
	private static volatile boolean vb = true;
	private static final AtomicBoolean ab = new AtomicBoolean(true);
	//
	private static final void setAB(final boolean pb) {
		ab.set(pb);
	}
	private static final void setVB(final boolean pb) {
		vb = pb;
	}
	private static final void setB(final boolean pb) {
		b = pb;
	}
	private static final synchronized void setBsync(final boolean pb) {
		b = pb;
	}
	//
	public static void main(String[] args) {
		final int TOTAL = (int) 1e8;
		long ts, diff;
		// Do anything
		System.out.println("B\t" + b);
		System.out.println("VB\t" + vb);
		System.out.println("AB\t" + ab.get());
		//
		// boolean
		ts = System.currentTimeMillis();
		for (int i = 0; i < TOTAL; i++) {
			setB(((i % 2) == 0) ? true : false);
		}
		diff = (System.currentTimeMillis() - ts);
		System.out.println("B\t" + TOTAL + "\t" + diff + "ms\t"
				+ (TOTAL / Math.max(diff / 1000, 1)) + "req/s\t"
				+ (TOTAL / Math.max(diff, 1)) + "req/ms");
		//
		// synchronized boolean 
		ts = System.currentTimeMillis();
		for (int i = 0; i < TOTAL; i++) {
			setBsync(((i % 2) == 0) ? true : false);
		}
		diff = (System.currentTimeMillis() - ts);
		System.out.println("Bsync\t" + TOTAL + "\t" + diff + "ms\t"
				+ (TOTAL / Math.max(diff / 1000, 1)) + "req/s\t"
				+ (TOTAL / Math.max(diff, 1)) + "req/ms");
		//
		// volatile boolean
		ts = System.currentTimeMillis();
		for (int i = 0; i < TOTAL; i++) {
			setVB(((i % 2) == 0) ? true : false);
		}
		diff = (System.currentTimeMillis() - ts);
		System.out.println("VB\t" + TOTAL + "\t" + diff + "ms\t"
				+ (TOTAL / Math.max(diff / 1000, 1)) + "req/s\t"
				+ (TOTAL / Math.max(diff, 1)) + "req/ms");
		//
		// AtomicBoolean
		ts = System.currentTimeMillis();
		for (int i = 0; i < TOTAL; i++) {
			setAB(((i % 2) == 0) ? true : false);
		}
		diff = (System.currentTimeMillis() - ts);
		System.out.println("AB\t" + TOTAL + "\t" + diff + "ms\t"
				+ (TOTAL / Math.max(diff / 1000, 1)) + "req/s\t"
				+ (TOTAL / Math.max(diff, 1)) + "req/ms");
		// Do anything
		System.out.println("B\t" + b);
		System.out.println("VB\t" + vb);
		System.out.println("AB\t" + ab.get());
	}
}

Referencias:
Patrones de uso para volatile
Managing volatility
Synchronized Methods
Atomic Variables

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: