Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- public class IncrementTest
- {
- public static void main(String[] args)
- {
- Stopper s = new Stopper();
- int a = 0;
- int b = 0;
- List<Long> timer = new ArrayList<>();
- List<Long> timer2 = new ArrayList<>();
- for(int o=0; o<1000000; o++){
- s.startNano();
- for(int i=0; i<Integer.MAX_VALUE; i++)
- ++a;
- s.stopNano();
- timer.add(s.getDiff());
- s.startNano();
- for(int i=0; i<Integer.MAX_VALUE; i++)
- b++;
- s.stopNano();
- timer2.add(s.getDiff());
- }
- double a1 = timer.stream().mapToLong(l -> (long) l).average().orElse(-1);
- double a2 = timer2.stream().mapToLong(l -> (long) l).average().orElse(-1);
- System.out.printf("preincrement:%2f\npostincrement:%2f\n", a1, a2);
- }
- }
- class Stopper{
- private long start, end, diff;
- public Stopper(){
- start = 0;
- end = 0;
- diff = -1;
- }
- public void start(){
- start = System.currentTimeMillis();
- }
- public void stop(){
- end = System.currentTimeMillis();
- diff = end - start;
- }
- public void startNano(){
- start = System.nanoTime();
- }
- public void stopNano(){
- end = System.nanoTime();
- diff = end - start;
- }
- public long getDiff(){ return diff; }
- }
Advertisement
Add Comment
Please, Sign In to add comment