[JAVA] Processing speed with and without static

static Because static has an image of shared property If you are always sitting in a corner of memory, I tried from the image that using static is faster.

let's try it

Processing speed measurement class.java


package mk42;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

public class StreamProcessingTest {
	private static final List<String> list = Arrays.asList("a", "s", "k");
	public static void main(String[] args) {
		Long start = System.nanoTime();
		test();
		Long end = System.nanoTime();
		Mk42.echo.accept("Processing TIme : " + (end - start) + "ns");
		/** ----------not static---------- */
		Mk42.echo.accept("next test");
		NotStaticProcessingTest no = new NotStaticProcessingTest();
		Long start2 = System.nanoTime();
		no.test2();
		Long end2 = System.nanoTime();
		Mk42.echo.accept("Processing TIme : " + (end2 - start2) + "ns");
	}
	private static void test() {
		// twice
		list.stream().forEach(v -> execute());
		list.stream().filter(v -> !v.equals("a")).forEach(v -> qwe());
		//The processing time does not change much whether multiple processing is performed inside the termination operation or divided..
	}
	private static void execute() {
		List<Integer> a = new ArrayList<Integer>();
		IntStream.range(0, 10).forEach(v -> a.add(2));
	}
	private static void qwe() {
		List<Integer> a = new ArrayList<Integer>();
		IntStream.range(0, 10000000).forEach(v -> a.add(2));
	}
}

A class that summarizes methods for non-static experiments.java


package mk42;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

public class NotStaticProcessingTest {
	private static final List<String> list = Arrays.asList("a", "s", "k");
	public void run() {
		System.out.println("qwe");
	}
	public void test() {
		// onece
		list.stream().forEach(v -> {
			execute();
			if(!v.equals("a"))
				qwe();
		});
		//The processing time does not change much whether multiple processing is performed inside the termination operation or divided..
	}
	public void test2() {
		// twice
		list.stream().forEach(v -> execute());
		list.stream().filter(v -> !v.equals("a")).forEach(v -> qwe());
		//The processing time does not change much whether multiple processing is performed inside the termination operation or divided..
	}
	private void execute() {
		List<Integer> a = new ArrayList<Integer>();
		IntStream.range(0, 10).forEach(v -> a.add(2));
	}
	private void qwe() {
		List<Integer> a = new ArrayList<Integer>();
		IntStream.range(0, 10000000).forEach(v -> a.add(2));
	}
}

result

static -> Processing TIme : 222787700ns
Non-static-> Processing TIme : 176494000ns

What? I thought, but when I experimented multiple times

static.


Processing TIme : 225677500ns
Processing TIme : 223625900ns
Processing TIme : 223150400ns
Processing TIme : 228877000ns
Processing TIme : 223187400ns

Non-static.


Processing TIme : 178923100ns
Processing TIme : 167760600ns
Processing TIme : 190593300ns
Processing TIme : 180728700ns
Processing TIme : 216358400ns

It's a bit annoying to average, but it's not I feel like there is little blurring

The strength of static is not that it is fast, but that there is little fluctuation in processing speed ...?

Recommended Posts

Processing speed with and without static
Christmas with Processing
[Terms] Static and dynamic
Link Processing and SQLite
The difference between programming with Ruby classes and programming without it
Mock static methods with PowerMock
[Java] Stack area and static area
Spring with Kotorin --6 Asynchronous processing
Common processing and error processing springmvc
Asynchronous processing with Shoryuken + SQS
Old and new speed showdown
Presentation slides made with Processing
Search and execute method by name from instance with processing (java)
I want to perform asynchronous processing and periodic execution with Rail !!!