Calculating Search Times

Posted on 2003-03-11
Medium Priority
Last Modified: 2011-10-03
Ok, I'm going crazy!

   I have a basic program to make.  It is suppose to calculate the average time it takes to do a search.  
My code looks like this:

//sequential search function
        //x = search value
     clock_t start, end;
     start = clock();
     for (i =0; i<n && CurrentArray[i] !=x; i++)
          if (i==n)
               exit (.1);     //ERROR! x not found!
     end = clock();
     return end-start;

I preform this search about numerous times, with randomly chosen x values .
ok so the problem I have is that no matter how large my array is, the return time is ALWAYS zero, IF I allow the function to run on it's on.  However if I debug it, executing the code line by line I am able to get none zero times.  
I have already recieved a 60 for this project, but have midterms soon!
Question by:wdrake_98
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 46

Accepted Solution

Kent Olsen earned 300 total points
ID: 8115531
The problem is: Your search is too fast.

It's completing before the system clock timer ticks to the next interval.

Try doing 1000 or 100,000 searches for the same item between checks of the clock and compute the search time from that.


Author Comment

ID: 8115712
Very FAST response!!!!
I just may have to sign up for the premium service!
It worked... had to increase it to like 1000, searches between the clocks but it worked...

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question