• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The Timing Doesn't Work

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My code compiles, and the searches work but the timings won't work, I purposely entered commands that would take extended times, but I still get outputs of 0ms. Obviously I've coded this part wrong but I can't seem to spot where I've gone wrong. I've attached where I think the problem lies. Can anyone see where I've messed up?

//** method for sequential search
public void sequential(int key){
int i;
time1 = System.currentTimeMillis();
for(i=0;i<size;i++){
countSequential++;
if(key==list[i])
break;
}
time2 = System.currentTimeMillis();
}


//** method for binary search
public void binary(int key){
int low=0;
int high=size-1;
int middle;
time3 = System.currentTimeMillis();
while(low<=high){
middle=(low+high)/2;
countBinary++;
if(key==list[middle])
break;
else if(key<list[middle])
high=middle-1;///search low end of the list
else
low=middle+1;///search higher end of the list
}
time4 = System.currentTimeMillis();
}

public void startsearch(){
int i;
for(i=0;i<count;i++){//searching each element once sequentially and then binary in the same list
sequential(list_search[i]);
binary(list_search[i]);
}
System.out.println("The number of comparisions done per element search:");
System.out.println("Sequential: "+countSequential/count);
System.out.println("Search Time: "+(time2 - time1));
System.out.println("Binary: "+countBinary/count);
System.out.println("Search Time: "+ (time4 - time3));
}

Thanks,
Neil
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How much data are you processing? One millisecond is still a relatively large amount of time for those algorithms.

Either change your program to process a lot of data, or try using the System.lang.nanoTime() method. Bear in mind though that nanoTime will just use the most precise timer available, which may not be any more precise than the one used for millisecond timing.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on the operating system, your System.currentTimeMillis may change in jumps as large as 55ms - why don't you write a loop to watch for a change in the returned value to see what granularity your system gives you.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic