Mail Archives: cygwin/1998/04/10/17:58:09
This is a multi-part message in MIME format.
--------------10E9B2677DA12DD1F4731536
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
clock() ?
It always return the same value - it was dirty value not constant ...
It doesn't measure time ...
Cezary
--------------10E9B2677DA12DD1F4731536
Content-Type: text/plain; charset=us-ascii; name="prime4.cc"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="prime4.cc"
#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;
}
--------------10E9B2677DA12DD1F4731536
Content-Type: text/plain; charset=us-ascii; name="Stopper.hh"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Stopper.hh"
// --------------------------------------------------------------------------
//
// 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 AT nov DOT iem DOT pw DOT edu DOT pl
// penta AT bsdi DOT iem DOT pw DOT edu DOT pl (recommended)
// wagnerc AT miriam DOT ee DOT pw DOT edu DOT 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__ */
--------------10E9B2677DA12DD1F4731536--
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -