This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

b19: clock() doesn't work !!!!!!!!!!!


clock() ?

It always return the same value - it was dirty value not constant ...
It doesn't measure time ...

Cezary


#include <iostream.h>
#include <ctime>
#include <list>
#include "stopper.hh"

const unsigned max_val = 200000;

int main()
{
        list<unsigned> pl;
        list<unsigned>::iterator i,j,k;
        unsigned u, n, isprime;

        Stopper st;

        st.Start();
        for(u = 2, n = 0, j = k = pl.begin();u != max_val;u++)
                {
                
				for(;j != pl.end()--;j++)
                        /* find last possible divisor for probable prime number */
                        /* current divisor must produce result great then its value
                           to be founded in prime list */
                        /* it based on that holes in prime list is even numbers or
                           numbers dividable by smaller primes than current
                           (see how prime list is builded up (from low to high and the
                            new prime is always greater value than divisors checken on it
                                for prime test commision)) */
                        if(u / *j < *j)
                                break;

                for(i = pl.begin();i != j;i++)
                        {
                        /* check if it not prime */
                        if(u % *i == 0)
                                break;                  /* not prime break */
                        }

                /* check if prime detected */
                if(i == j)
                        {
						n++;
						isprime = 1;
                        pl.insert(pl.end(),u);

						/* fix iterator for first prime from end() position
						   which will be undefinied by inserting first element
						   to the list */
                        if(pl.size() == 1)
                                j = k = pl.begin();
/*                      cout << u << "(" << pl.size() << ")("
                                << *j << ")(" << u / *j << ")\n"; */
                        }
                /* flushing entries every 1000 */
                if(n % 1000 == 0 && n && isprime)
                        {
				        list<unsigned>::iterator last = pl.end();
						st.Stop();
								isprime = 0;
                                cout << *--last << "\n"
									 << clock() << "\n";
						st.Start();
                        }
                }
        st.Stop();
        cout << "time = " << st.Time()
             << " speed = " << (u - 1) / st.Time() << "\n";

        return 0;
}
// --------------------------------------------------------------------------
//
// Name    : stopper.hh.
// Date    : 1996-08-14
// Autor   : Cezary Wagner
// RCS     : $Id: stopper.hh 1.1.1.3 1997/01/04 14:33:44 dosuser Exp $
// Contact : penta@nov.iem.pw.edu.pl
//           penta@bsdi.iem.pw.edu.pl (recommended)
//           wagnerc@miriam.ee.pw.edu.pl (also good)
// Summary : Stopper like you wear on your hand
// Notes   : 
//
// --------------------------------------------------------------------------

#ifndef __STOPPER_HH__
#define __STOPPER_HH__

#include <ctime>

class Stopper
{
	clock_t start_time;
	clock_t stop_time;
	unsigned is_stoped : 1;
public:
	Stopper() { Reset(); }

	inline void Start();
	inline double Time();
	inline void Stop();
	inline void Reset();
};

// --------------------------------------------------------------------------
// Inline definition
// --------------------------------------------------------------------------

void Stopper::Start()
{
	if(is_stoped)
		// if stopper is stopped add time meassured before
		start_time = clock() - (stop_time - start_time);
	is_stoped = 0;
}

double Stopper::Time()
{
	clock_t now_time;

	if(is_stoped)
		// return meassured time
		return (stop_time - start_time) / (double)CLOCKS_PER_SEC;
	// return meanwhile time
	now_time = clock();
	return (now_time - start_time) / (double)CLOCKS_PER_SEC;
}

void Stopper::Stop()
{
	stop_time = clock();
	is_stoped = 1;
}

void Stopper::Reset()
{
	start_time = 0;
	stop_time = 0;
	is_stoped = 1;
}

#endif /* __STOPPER_HH__ */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]