Java/Timing: Difference between revisions
From charlesreid1
| Line 39: | Line 39: | ||
In the Java repository ([http://git.charlesreid1.com/cs/java link]) in the lists/linked-lists folder ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists link]), there is a timing script ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists/Timing.java link]) that demonstrates a simple comparison of the built-in linked list type to a trimmed down generic TLinkedList type ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists/TLinkedList.java link]). | In the Java repository ([http://git.charlesreid1.com/cs/java link]) in the lists/linked-lists folder ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists link]), there is a timing script ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists/Timing.java link]) that demonstrates a simple comparison of the built-in linked list type to a trimmed down generic TLinkedList type ([https://charlesreid1.com:3000/cs/java/src/master/lists/linked-lists/TLinkedList.java link]). | ||
<pre> | |||
public class Timing { | |||
// Tests | |||
public static void main(String[] args) { | |||
linked_list_add_test(); | |||
} | |||
/** Compare the add method - appending to the rear of a list. | |||
* | |||
* Compares builtin LinkedList type with self-authored TLinkedList class. | |||
* */ | |||
public static void linked_list_add_test() { | |||
System.out.println("N, Walltime Builtin (s), Walltime Mine (s)"); | |||
// Loop over some values of N, and print the amortized operational cost. | |||
for(int N = 1024; N < 1E6; N*=2) { | |||
int ntrials = 1000; | |||
// Hold on to your butts. | |||
System.out.printf("%d, ", N); | |||
// builtin linked list: | |||
// | |||
// b prefix is for built-in datatype | |||
Tim btim = new Tim(); | |||
// Trials counter is always | |||
// K for Kafka | |||
for(int k = 0; k<ntrials; k++) { | |||
LinkedList<Integer> blist = new LinkedList<Integer>(); | |||
for(int i=0; i<N; i++) { | |||
blist.add(i*i); | |||
} | |||
} | |||
double btime_total = btim.elapsed(); | |||
System.out.printf("%.2f,", btime_total); | |||
// my little linked list: | |||
// | |||
// m prefix is for mine | |||
Tim mtim = new Tim(); | |||
// Trials counter is k | |||
for(int k=0; k<ntrials; k++) { | |||
TLinkedList<Integer> mylist = new TLinkedList<Integer>(); | |||
for(int i=0; i<N; i++) { | |||
mylist.addLast(i*i); | |||
} | |||
} | |||
double mtime_total = mtim.elapsed(); | |||
System.out.printf("%.2f \n", mtime_total); | |||
} | |||
} | |||
} | |||
</pre> | |||
==Flags== | ==Flags== | ||
Revision as of 01:54, 5 June 2017
If you want an extremely detailed picture of how much time you're spending in the various parts of your code, you can use a profiler: see Java/Profiling
Basic Timing in Java: Builtin Methods
If you just want to see how much time a piece of code takes to execute, you can use Java's built in time functionality:
long start = System.nanoTime();
doStuff();
long end = System.nanoTime();
long duration = end - start;
System.out.printf("Elapsed time: %03f s\n", duration/1E9);
Timing Snippets of Code
Can make a Stopwatch class that does the following:
- Constructor creates a new "start" variable - the stopwatch class measures time starting at its own creation
- Can call elapsed() method to get elapsed seconds
Via http://introcs.cs.princeton.edu/java/32class/Stopwatch.java.html
public class Stopwatch {
public Stopwatch() {
this.start = System.currentTimeMillis();
}
public double elapsed() {
this.end = System.currentTimeMillis();
return (end-start)/1000.0;
}
}
Comparing Hombrew Data Containers to Java Collections API
In the Java repository (link) in the lists/linked-lists folder (link), there is a timing script (link) that demonstrates a simple comparison of the built-in linked list type to a trimmed down generic TLinkedList type (link).
public class Timing {
// Tests
public static void main(String[] args) {
linked_list_add_test();
}
/** Compare the add method - appending to the rear of a list.
*
* Compares builtin LinkedList type with self-authored TLinkedList class.
* */
public static void linked_list_add_test() {
System.out.println("N, Walltime Builtin (s), Walltime Mine (s)");
// Loop over some values of N, and print the amortized operational cost.
for(int N = 1024; N < 1E6; N*=2) {
int ntrials = 1000;
// Hold on to your butts.
System.out.printf("%d, ", N);
// builtin linked list:
//
// b prefix is for built-in datatype
Tim btim = new Tim();
// Trials counter is always
// K for Kafka
for(int k = 0; k<ntrials; k++) {
LinkedList<Integer> blist = new LinkedList<Integer>();
for(int i=0; i<N; i++) {
blist.add(i*i);
}
}
double btime_total = btim.elapsed();
System.out.printf("%.2f,", btime_total);
// my little linked list:
//
// m prefix is for mine
Tim mtim = new Tim();
// Trials counter is k
for(int k=0; k<ntrials; k++) {
TLinkedList<Integer> mylist = new TLinkedList<Integer>();
for(int i=0; i<N; i++) {
mylist.addLast(i*i);
}
}
double mtime_total = mtim.elapsed();
System.out.printf("%.2f \n", mtime_total);
}
}
}