Skip to content

Instantly share code, notes, and snippets.

@ignatov
Created July 16, 2019 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ignatov/dc60aa3e3def823d18711e8db61aae2f to your computer and use it in GitHub Desktop.
Save ignatov/dc60aa3e3def823d18711e8db61aae2f to your computer and use it in GitHub Desktop.
Ref/Invoke via reflection
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