Why does it run so long?

M

Michael

Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

Thanks for any help.

Yours sincerely,

Michael

P.S.:
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between
0 and 1;
if(check(x)==0) goto mark;
for(j=1; j<=100; j++) A[j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x<0)||(x>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array*x;
if(sum != 0) return 0;
return 1;
}
 
N

Nick Keighley

Michael said:
Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

Thanks for any help.

Yours sincerely,

Michael

P.S.:
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between
0 and 1;
if(check(x)==0) goto mark;

you have a goto here that depends on the output of a random number
generator.
If you can work out an upper bound on the number of times this loops
you're smarter than me (not hard).

What is it supposed to do? Why do you use a goto?

for(j=1; j<=100; j++) A[j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x<0)||(x>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array*x;
if(sum != 0) return 0;
return 1;
}



--
Nick Keighley

"Anyone attempting to generate random numbers by deterministic
means is, of course, living in a state of sin."
-- John Von Neumann
 
E

Eric Sosman

Michael said:
Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

It's hard to tell for sure, since you haven't provided
code that will compile: you've omitted the definitions of
`C', or `N', and of `array', and given no clue to their
values. (For example, if the Random() function behaves as
you say and if C has a negative value, it's very easy to
see why the program will run for a long time ...)

Even so, an incomplete diagnosis is possible. One of
your problems is that you haven't read and understood
Questions 14.4 and 14.5 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
C

Charles M. Reinke

Michael said:
Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

[snip]

for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between
0 and 1;
if(check(x)==0) goto mark;
for(j=1; j<=100; j++) A[j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x<0)||(x>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array*x;
if(sum != 0) return 0;


Try this line instead, and see whether or not it dramatically decreases run
time:
if(sum>SUM_EPS) return 0;
where SUM_EPS is #defined as something like 0.0001 (or some other, suitable
small, epsilon).
return 1;
}

The problem may be that for many interations "sum" comes out to something
like 0.0000438764, which for your purposes *equals* 0. However, since
that's not actually equal to 0 (to your processor, anyway), your "check"
function returns 0 and the program loops again.

In fact, as far as I can tell, unless "array[]" is full of 0s your code is
dependent upon floating-point underflow to ever complete. IMHO depending on
underflow for program completion is usually not a Good Thing.

-Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top