0

I'm working on comparing a Binary Search Tree to an AVL one and want to see the usr/sys time for a search operation performed on both. Thing is: I have an application (SearchBST.java/SearchAVL.java) that reads in a file and populates the trees, and then searches them. I want to know if I can check the usr/sys time for just the searching instead of the entire thing (inserting and searching). It seems to me that the insertion is causing the AVL's time (using "time java SearchAVL") to be roughly the same as the BST's.

Should I be doing it differently (such that populating the tree doesn't affect the overall time)? I'll post some code as soon as I can, but I wanted to see if anyone has any thoughts in the mean time.

Paul12596
  • 300
  • 3
  • 13

1 Answers1

0

Why don't you measure the time inside your application?

// Read file to a temporary collection or array
// to prevent meassuring disk performance instead of tree performance

long t = System.nanoTime();
// populate tree
long tPopulate = System.nanoTime() - t;

t = System.nanoTime();
// search tree
long tSearch = System.nanoTime() - t;

System.out.println("tPopulate = " + tPopulate + " ns");
System.out.println("tSearch   = " + tSearch + " ns");

This will only print the wall clock time, but since you don't have any Thread.sleep(...) commands or things like that in your program, the wall clock time shouldn't differ much from the user time.

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • That won't tell them the user/sys breakdown (though you wouldn't expect much sys time in a tree search). See also [*How do I write a correct micro-benchmark in Java?*](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – T.J. Crowder Apr 18 '17 at 08:14
  • It actually just occurred to me to just take the time for population, then take the time for pop+search, then calculate the difference. This doesn't feel like the cleanest, most interesting solution, but I can't see why it wouldn't work off the bat. – Paul12596 Apr 18 '17 at 09:05
  • Regardless of the above, it seems like the "do it in the program" might be the way to go. Thanks! (I'll probably use Instant, though). – Paul12596 Apr 18 '17 at 09:33