Why does it run so long?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    #1

    Why does it run so long?

    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[i][j]=x[j];
    }

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







  • Nick Keighley

    #2
    Re: Why does it run so long?

    Michael wrote:
    [color=blue]
    > 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;[/color]

    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?

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


    --
    Nick Keighley

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

    Comment

    • Eric Sosman

      #3
      Re: Why does it run so long?

      Michael wrote:[color=blue]
      > 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?[/color]

      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



      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Charles M. Reinke

        #4
        Re: Why does it run so long?

        "Michael" <ylftestspam@12 6.com> wrote in message
        news:df6pdg$2u4 f$2@mail.cn99.c om...[color=blue]
        > 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?
        >[/color]

        [snip]
        [color=blue]
        > for(i=1; i<=30; i++)
        > {
        > mark:
        > for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value[/color]
        between[color=blue]
        > 0 and 1;
        > if(check(x)==0) goto mark;
        > for(j=1; j<=100; j++) A[i][j]=x[j];
        > }
        >
        > where check() is£º
        > static int check(double x[])
        > {
        > for(i = 1; i <= 100; i++)
        > if((x[i]<0)||(x[i]>C) return 0;
        > double sum = 0;
        > for(i = 1; i <= N; i++)
        > sum += array[i]*x[i];
        > if(sum != 0) return 0;[/color]

        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).
        [color=blue]
        > return 1;
        > }
        >[/color]

        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


        Comment

        Working...