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

java.lang.NoClassDefFoundError: IllegalName
Change programmatically color of title bar of dialog, color is not pre-defined. Android [duplicate]
How to create unmanaged Neo4j extension?
Limit number of distinct items for any given period of time
How to set common interest for omegle (Android)
Iterating through Resultsets in threads cause Exceptions. ResultSets of queries to Hive using JDBC connections
cloudinary java not able to upload image
Java AlphaComposite blending massive quality loss
Java Scanner Breaks when an unknown thing is input
How do i run java from a different directory?
android: Creating Image and filling colors in Canvas
Redisplay the menu [closed]
multi threading synchronization issue
Does JAX-RS Support Input Streaming?
Update Jmeter variables with beanshell
Java Consume SharePoint 2010 REST Service with Domain Authentication

Categories

HOME
xamarin
image
urbancode
plot
layout
hashmap
vmware
v8
django-imagekit
adobe-analytics
n-gram
ups
msp430
apache-metamodel
crystal-reports-2008
ml
wpfdatagrid
maxmind
compatibility
atlassian-crucible
librato
bluestacks
javascriptcore
http-status-code-503
titanium-mobile
calibre
uiswipegesturerecognizer
nssegmentedcontrol
bower-install
restlet
scaffold
code-search-engine
cookiecutter-django
http-live-streaming
btrace
angular-resource
strptime
atomicity
jlink
domain-model
pebble-watch
feeds
knpmenubundle
dynamics-sl
vtigercrm
cubes
xcb
nxt
probability-density
skobbler-maps
connect-by
fadeout
hexdump
asp.net5
freelancer.com-api
ios8-today-widget
computer-algebra-systems
angular-leaflet-directive
embedded-code
nsight
directoryservices
angularjs-ng-click
java.util.concurrent
codeigniter-routing
contenttype
comexception
marmalade
kgdb
oracle-warehouse-builder
viadeo
ccss
tws
excel-2003
jquery-mobile-dialog
mqx
isnullorempty
datawindow
regsvr32
windows-phone-7.1.1
mercurial-server
html-input
zend-translate
dentrix
filtered-index
yagni
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App