From charlesreid1

(Created page with "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 If you just want...")
 
m (Replacing charlesreid1.com:3000 with git.charlesreid1.com)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
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]]
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:
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:


<source lang="java">
<pre>
long start = System.nanoTime();
long start = System.nanoTime();
doStuff();
doStuff();
Line 9: Line 11:
long duration = end - start;
long duration = end - start;
System.out.printf("Elapsed time: %03f s\n", duration/1E9);
System.out.printf("Elapsed time: %03f s\n", duration/1E9);
</source>
</pre>
 
 
==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
 
<pre>
public class Stopwatch {
    public Stopwatch() {
        this.start = System.currentTimeMillis();
    }
    public double elapsed() {
        this.end = System.currentTimeMillis();
        return (end-start)/1000.0;
    }
}
</pre>
 
 
 
==Comparing Hombrew Data Containers to Java Collections API==
 
In the Java repository ([http://git.charlesreid1.com/cs/java link]) in the lists/linked-lists folder ([https://git.charlesreid1.com/cs/java/src/master/lists/linked-lists link]), there is a timing script ([https://git.charlesreid1.com/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://git.charlesreid1.com/cs/java/src/master/lists/linked-lists/TLinkedList.java link]).
 
See [[Linked Lists/Java/Timing]] for a timing comparison of the Collections Linked List type and a hand-rolled, generic Linked List type.
 
[[Image:LinkedListProfiling_Add.png|500px]]
 
[[Image:LinkedListProfiling_AddRemove.png|500px]]
 
Demonstrations of the O(1) behavior of adding, and randomly adding/removing, using a linked list.
 
==Flags==
 
[[Category:Java]]
[[Category:Profiling]]
[[Category:Programming]]
[[Category:Timing]]

Latest revision as of 03:33, 9 October 2019

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).

See Linked Lists/Java/Timing for a timing comparison of the Collections Linked List type and a hand-rolled, generic Linked List type.

LinkedListProfiling Add.png

LinkedListProfiling AddRemove.png

Demonstrations of the O(1) behavior of adding, and randomly adding/removing, using a linked list.

Flags