Time how long a function runs (short duration)

Multi tool use
I'm relatively new to Java programming, and I'm running into an issue calculating the amount of time it takes for a function to run.
First some background - I've got a lot of experience with Python, and I'm trying to recreate the functionality of the Jupyter Notebook/Lab %%timeit
function, if you're familiar with that. Here's a pic of it in action (sorry, not enough karma to embed yet):
Snip of Jupyter %%timeit
What it does is run the contents of the cell (in this case a recursive function) either 1k, 10k, or 100k times, and give you the average run time of the function, and the standard deviation.
My first implementation (using the same recursive function) used System.nanoTime()
:
public static void main(String args)
long t1, t2, diff;
long times = new long[1000];
int t;
for (int i=0; i< 1000; i++)
t1 = System.nanoTime();
t = triangle(20);
t2 = System.nanoTime();
diff = t2-t1;
System.out.println(diff);
times[i] = diff;
long total = 0;
for (int j=0; j<times.length; j++)
total += times[j];
System.out.println("Mean = " + total/1000.0);
But the mean is wildly thrown off -- for some reason, the first iteration of the function (on many runs) takes upwards of a million nanoseconds:
Pic of initial terminal output
Every iteration after the first dozen or so takes either 395 nanos or 0 -- so there could be a problem there too... not sure what's going on!
Also -- the code of the recursive function I'm timing:
static int triangle(int n)
if (n == 1)
return n;
else
return n + triangle(n -1);
Initially I had the line n = Math.abs(n)
on the first line of the function, but then I removed it because... meh. I'm the only one using this.
I tried a number of different suggestions brought up in this SO post, but they each have their own problems... which I can go into if you need.
Anyway, thank you in advance for your help and expertise!
java time benchmarking nanotime
|
show 1 more comment
I'm relatively new to Java programming, and I'm running into an issue calculating the amount of time it takes for a function to run.
First some background - I've got a lot of experience with Python, and I'm trying to recreate the functionality of the Jupyter Notebook/Lab %%timeit
function, if you're familiar with that. Here's a pic of it in action (sorry, not enough karma to embed yet):
Snip of Jupyter %%timeit
What it does is run the contents of the cell (in this case a recursive function) either 1k, 10k, or 100k times, and give you the average run time of the function, and the standard deviation.
My first implementation (using the same recursive function) used System.nanoTime()
:
public static void main(String args)
long t1, t2, diff;
long times = new long[1000];
int t;
for (int i=0; i< 1000; i++)
t1 = System.nanoTime();
t = triangle(20);
t2 = System.nanoTime();
diff = t2-t1;
System.out.println(diff);
times[i] = diff;
long total = 0;
for (int j=0; j<times.length; j++)
total += times[j];
System.out.println("Mean = " + total/1000.0);
But the mean is wildly thrown off -- for some reason, the first iteration of the function (on many runs) takes upwards of a million nanoseconds:
Pic of initial terminal output
Every iteration after the first dozen or so takes either 395 nanos or 0 -- so there could be a problem there too... not sure what's going on!
Also -- the code of the recursive function I'm timing:
static int triangle(int n)
if (n == 1)
return n;
else
return n + triangle(n -1);
Initially I had the line n = Math.abs(n)
on the first line of the function, but then I removed it because... meh. I'm the only one using this.
I tried a number of different suggestions brought up in this SO post, but they each have their own problems... which I can go into if you need.
Anyway, thank you in advance for your help and expertise!
java time benchmarking nanotime
1
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried theInstant
andDuration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?
– Luke
Nov 14 '18 at 1:19
Like python'stimeit
(which you attributed to Jupyter)? No. But there isStopWatch
in Apache Commons Lang.
– Elliott Frisch
Nov 14 '18 at 1:28
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the pythontimeit
function, yes.
– Luke
Nov 14 '18 at 1:38
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?
– Ole V.V.
Nov 14 '18 at 7:21
|
show 1 more comment
I'm relatively new to Java programming, and I'm running into an issue calculating the amount of time it takes for a function to run.
First some background - I've got a lot of experience with Python, and I'm trying to recreate the functionality of the Jupyter Notebook/Lab %%timeit
function, if you're familiar with that. Here's a pic of it in action (sorry, not enough karma to embed yet):
Snip of Jupyter %%timeit
What it does is run the contents of the cell (in this case a recursive function) either 1k, 10k, or 100k times, and give you the average run time of the function, and the standard deviation.
My first implementation (using the same recursive function) used System.nanoTime()
:
public static void main(String args)
long t1, t2, diff;
long times = new long[1000];
int t;
for (int i=0; i< 1000; i++)
t1 = System.nanoTime();
t = triangle(20);
t2 = System.nanoTime();
diff = t2-t1;
System.out.println(diff);
times[i] = diff;
long total = 0;
for (int j=0; j<times.length; j++)
total += times[j];
System.out.println("Mean = " + total/1000.0);
But the mean is wildly thrown off -- for some reason, the first iteration of the function (on many runs) takes upwards of a million nanoseconds:
Pic of initial terminal output
Every iteration after the first dozen or so takes either 395 nanos or 0 -- so there could be a problem there too... not sure what's going on!
Also -- the code of the recursive function I'm timing:
static int triangle(int n)
if (n == 1)
return n;
else
return n + triangle(n -1);
Initially I had the line n = Math.abs(n)
on the first line of the function, but then I removed it because... meh. I'm the only one using this.
I tried a number of different suggestions brought up in this SO post, but they each have their own problems... which I can go into if you need.
Anyway, thank you in advance for your help and expertise!
java time benchmarking nanotime
I'm relatively new to Java programming, and I'm running into an issue calculating the amount of time it takes for a function to run.
First some background - I've got a lot of experience with Python, and I'm trying to recreate the functionality of the Jupyter Notebook/Lab %%timeit
function, if you're familiar with that. Here's a pic of it in action (sorry, not enough karma to embed yet):
Snip of Jupyter %%timeit
What it does is run the contents of the cell (in this case a recursive function) either 1k, 10k, or 100k times, and give you the average run time of the function, and the standard deviation.
My first implementation (using the same recursive function) used System.nanoTime()
:
public static void main(String args)
long t1, t2, diff;
long times = new long[1000];
int t;
for (int i=0; i< 1000; i++)
t1 = System.nanoTime();
t = triangle(20);
t2 = System.nanoTime();
diff = t2-t1;
System.out.println(diff);
times[i] = diff;
long total = 0;
for (int j=0; j<times.length; j++)
total += times[j];
System.out.println("Mean = " + total/1000.0);
But the mean is wildly thrown off -- for some reason, the first iteration of the function (on many runs) takes upwards of a million nanoseconds:
Pic of initial terminal output
Every iteration after the first dozen or so takes either 395 nanos or 0 -- so there could be a problem there too... not sure what's going on!
Also -- the code of the recursive function I'm timing:
static int triangle(int n)
if (n == 1)
return n;
else
return n + triangle(n -1);
Initially I had the line n = Math.abs(n)
on the first line of the function, but then I removed it because... meh. I'm the only one using this.
I tried a number of different suggestions brought up in this SO post, but they each have their own problems... which I can go into if you need.
Anyway, thank you in advance for your help and expertise!
java time benchmarking nanotime
java time benchmarking nanotime
edited Nov 14 '18 at 7:20


Ole V.V.
29.6k63653
29.6k63653
asked Nov 14 '18 at 1:06


LukeLuke
12
12
1
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried theInstant
andDuration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?
– Luke
Nov 14 '18 at 1:19
Like python'stimeit
(which you attributed to Jupyter)? No. But there isStopWatch
in Apache Commons Lang.
– Elliott Frisch
Nov 14 '18 at 1:28
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the pythontimeit
function, yes.
– Luke
Nov 14 '18 at 1:38
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?
– Ole V.V.
Nov 14 '18 at 7:21
|
show 1 more comment
1
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried theInstant
andDuration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?
– Luke
Nov 14 '18 at 1:19
Like python'stimeit
(which you attributed to Jupyter)? No. But there isStopWatch
in Apache Commons Lang.
– Elliott Frisch
Nov 14 '18 at 1:28
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the pythontimeit
function, yes.
– Luke
Nov 14 '18 at 1:38
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?
– Ole V.V.
Nov 14 '18 at 7:21
1
1
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried the
Instant
and Duration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?– Luke
Nov 14 '18 at 1:19
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried the
Instant
and Duration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?– Luke
Nov 14 '18 at 1:19
Like python's
timeit
(which you attributed to Jupyter)? No. But there is StopWatch
in Apache Commons Lang.– Elliott Frisch
Nov 14 '18 at 1:28
Like python's
timeit
(which you attributed to Jupyter)? No. But there is StopWatch
in Apache Commons Lang.– Elliott Frisch
Nov 14 '18 at 1:28
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the python
timeit
function, yes.– Luke
Nov 14 '18 at 1:38
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the python
timeit
function, yes.– Luke
Nov 14 '18 at 1:38
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?– Ole V.V.
Nov 14 '18 at 7:21
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?– Ole V.V.
Nov 14 '18 at 7:21
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291741%2ftime-how-long-a-function-runs-short-duration%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291741%2ftime-how-long-a-function-runs-short-duration%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
hxC,fsKv4gHqkLH2NJgxkV,bQv33WJ0 Kufo
1
JIT. Warm up. Benchmarking is hard. System clocks don't really do nano second resolution. What platform is this anyway? Which version of Java, which JRE, which operating system, what results do you expect?
– Elliott Frisch
Nov 14 '18 at 1:15
Using JDK10 + the JRE that came with it -- running on Windows 10. Is there a better timer to use? I tried the
Instant
andDuration
ones mentioned in that other post, same problem there too. Edit: does the JVM come with its own timer?– Luke
Nov 14 '18 at 1:19
Like python's
timeit
(which you attributed to Jupyter)? No. But there isStopWatch
in Apache Commons Lang.– Elliott Frisch
Nov 14 '18 at 1:28
I should have said -- does the JVM come with its own internal clock that I can reach out to, if the system clock is a bad way to go about this. I was referring to the Jupyter magic command, which uses the python
timeit
function, yes.– Luke
Nov 14 '18 at 1:38
System.nanoTime
is the right method to use. You get nothing preciser. Related: How do I write a correct micro-benchmark in Java?– Ole V.V.
Nov 14 '18 at 7:21