Created
July 16, 2019 22:04
-
-
Save ignatov/dc60aa3e3def823d18711e8db61aae2f to your computer and use it in GitHub Desktop.
Ref/Invoke via reflection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
public class Main { | |
public static void main(String[] args) throws Throwable { | |
ArrayList<String> ref = new ArrayList<String>(); | |
ref.add("public class Ref {\n"); | |
ref.add(" public static void main(String[] args) {\n"); | |
for (int i = 0; i < 5_000; i++) { | |
ref.add(" new s.S" + i + "();"); | |
Files.write(Paths.get("src/s/S" + i + ".java"), ("package s; public class S" + i + "{}").getBytes()); | |
} | |
ref.add("\n }\n}"); | |
Files.write(Paths.get("src/Ref.java"), ref); | |
measure(() -> Ref.main(args), "Ref"); | |
// measure(() -> { for (int i = 0; i < 5000; i++) Class.forName("s.S" + i).getConstructor().newInstance(); }, "Inv"); | |
} | |
private static void measure(ThrowableRunnable run, String what) throws Throwable { | |
long start = System.currentTimeMillis(); | |
run.run(); | |
long end = System.currentTimeMillis(); | |
System.out.println(what + ":" + (end - start)); | |
} | |
public interface ThrowableRunnable<T extends Throwable> { | |
void run() throws T; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment