Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.tuis.ubench.UBench;
- public class RevList {
- private static class Node {
- final int value;
- Node next;
- public Node(int val, Node nxt) {
- this.value = val;
- this.next = nxt;
- }
- }
- public static Node buildList(int size) {
- Node next = null;
- while (size > 0) {
- next = new Node(size--, next);
- }
- return next;
- }
- public static Node rReverse(Node node) {
- if (node == null || node.next == null) {
- return node;
- }
- Node next = node.next;
- node.next = null;
- Node last = rReverse(next);
- next.next = node;
- return last;
- }
- public static Node iReverse(Node node) {
- Node previous = null;
- while (node != null) {
- Node next = node.next;
- node.next = previous;
- previous = node;
- node = next;
- }
- return previous;
- }
- public static void main(String[] args) {
- final int max = 10000;
- UBench bench = new UBench("Comparative Performance")
- .addIntTask("Recursive", () -> rReverse(buildList(max)).value, v -> v == max)
- .addIntTask("Iterative", () -> iReverse(buildList(max)).value, v -> v == max);
- bench.press(3000).report("Recursion or Iterative");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement