java


Timing method inside multiple for in java


i am having trouble with timing two methods that are included inside multiple for. I am using System.currentTimeMillis(); . The problem is that i don't know where to put start time and end time. I want to time 5 executions of methodA and methodB.
When i run it i get 12 ms for methodA and 0ms for methodB but the program takes about 10 minutes to run. The code is like below:
int count = 0;
double e = 0.3;
long startTimeA;
long endTimeA;
long startTimeB;
long endTimeB;
long sumA = 0;
long sumB = 0;
for(int k=0; k<5; k++){
startTimeA = System.currentTimeMillis();
startTimeB = System.currentTimeMillis();
for(int i = 0; i < 40; i++) {
for(int j = i + 1; j < 40; j++) {
double a = methodA();
endTimeA = System.currentTimeMillis() - startTimeA;
double b = methodB();
endTimeB = System.currentTimeMillis() - startTimeB;
int c = methodC();
}
sumA += endTimeA;
sumB += entTimeB;
}
}
System.out.println("total time A: " + sumA + " ms");
System.out.println("total time B: " + sumB + " ms");
You should read about the micro-benchmarking and how to do it in practice.
Here is a way to accomplish what you are trying to do:
private void doSomething() {
long startTimeA = System.currentTimeMillis();
executeA();
long endTimeA = System.currentTimeMillis();
System.out.println("Total Time A: " + (endTimeA - startTimeA));
long startTimeB = System.currentTimeMillis();
executeB();
long endTimeB = System.currentTimeMillis() - startTimeB;
System.out.println("Total Time B: " + (endTimeB - startTimeB));
}
private void executeA() {
for(int k=0; k<5; k++) {
for(int i = 0; i < 40; i++) {
for(int j = i + 1; j < 40; j++) {
double a = methodA();
}
}
}
}
private void executeB() {
for(int k=0; k<5; k++) {
for(int i = 0; i < 40; i++) {
for(int j = i + 1; j < 40; j++) {
double b = methodB();
}
}
}
}
for(int k=0; k<5; k++){
sumA = 0;
sumB = 0;
for(int i = 0; i < 40; i++) {
for(int j = i + 1; j < 40; j++) {
startTimeA = System.currentTimeMillis();
double a = methodA();
endTimeA = System.currentTimeMillis() - startTimeA;
sumA += endTimeA;
startTimeB = System.currentTimeMillis();
double b = methodB();
endTimeB = System.currentTimeMillis() - startTimeB;
sumB += endTimeB;
int c = methodC();
}
}
System.out.println("total time A: " + sumA + " ms");
System.out.println("total time B: " + sumB + " ms");
}
Just move
sumA += endTimeA;
sumB += entTimeB;
into "for j" loop.

Related Links

saving console without changing System.out.println(“blabla”); [closed]
Not able to resolve the “NullPointerException”
Java Die, Counting Snake Eyes
Lowercase 'g' in a string is “happy” if there is another 'g' immediately next to it
How to save state on a class which extends ImageView?
String, Substring, and if-else Statements in Change- Making Program
try-catch that will catch if user input is incorrect
Best way to escape quotes in dto string field in Spring MVC using taglibs
How to translate this List to Java?
Apply Generic logic for different types in Java
Java Linked List: How to add a Pixel in a Picture
Spring Data JPA ignores FetchMode
File not writing when java program is executed from command line
Spring JPA + EclipseLink ID generated in trigger
Creating a table programmatically using MyBatis and MySql
Legacy beans in a JavaFX TableView not updating

Categories

HOME
jdo
clips
atom-editor
pypi
office365api
tesseract
cplex
octobercms
rsyslog
at-command
fsm
icloud
constraint-programming
vifm
nstableview
decomposition
quartz-scheduler
jsdoc
flux
normalizr
oxyplot
firefox-webextensions
cloudhub
ehcache
blazemeter
shopware
xpages-ssjs
dynamic-featured-image
pingfederate
nhapi
applozic
fluentvalidation
file-rename
lightswitch-2013
iframe-resizer
siesta-swift
scorm2004
jspresso
framemaker
phpfreechat
nssegmentedcontrol
ios5
io-redirection
web-mining
hot-module-replacement
swift3.0.2
dynamics-crm-2013
trim
mesos-chronos
ws-security
google-closure
outlook-api
netcdf4
s
apache-fop
strptime
tropo
drupal-6
firmata
spring-android
radtreelist
phishing
skobbler-maps
connect-by
rvest
ios8-today-widget
reactive-banana
marching-cubes
feedback
tt-news
php-5.4
elliptic-curve
braille
concurrent-collections
fmod
terminal-services
gnu-smalltalk
ivyde
bulkloader
qt-faststart
seed
xamlparseexception
plasma
angularjs-controller
gil
bluepill
manchester-syntax
automount
errai
viewswitcher
removeclass
bigcouch
gwt-ext
pydot
heartbeat
subgurim-maps
xtype
appendto
imac
mongomapper
floating
sudzc
h.323
bespin
firefox4
nintendo-ds
photoshop-cs4
ti-dsp
aquaticprime
paul-graham

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App