I want to make a test, what is more efficient write to a regular variable or write to volatile variable. I wrote this code for the test
private int v1= 42;
private volatile int v2 = 43;
public void measure(){
long startRegular = System.nanoTime();
v1++;
long endRegular= System.nanoTime();
v2++;
long endVoltaile = System.nanoTime();
long rt = endRegular - startRegular;
long vt = endVoltaile - endRegular;
System.out.println("regular time : " + rt + " voltaile time " + vt);
}
The output from this program is diffrenet result for every run. Is this a valid test? or there is a better way to test it?