Results 1 to 8 of 8

Thread: Java Program

  1. #1
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default Java Program

    hello , if I write this program
    Java Code:
    Product
    integerx, y, m, n, z;
    input (x, y);
    if(x > y) {
    m = x; n = y;
    } else {
    m = y; n = x;
    };
    z = 0;
    while(n > 0) {
    z = z + m;
    n = n -1;
    }
    output (z);
    the fact that it choose the first factor of multiplication the bigger between them, avoid the present of multiplication just like 2*1000 , that has many calculations 2;4,6;8;10; etc.. , jumping directly to 2000 . But does the presence of few calculations really influence the processor during the his job, how much it encumbered by the presence of many calculations?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Processor is plenty powerful enough to pull this. Most performance issues for beginners are:

    1. Get something to work first.
    2. Maybe a lot of disk writes slow things some, but no big deal.

    In that order.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    I know the processor is powerfull enough but If I run this instruction in a loop for some thousands of times , do the two way have a different impact on the processor, I mean in long time?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Use System.nanoTime() to get current times before and after the thousands of times and print the differences between doing it one way and the other. Then you'll know which is faster.
    Come back and post the results here.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post nano-nano in a loop should run seveal thousand iterations

    Java Code:
        /**
         * Returns the current value of the most precise available system
         * timer, in nanoseconds.
         *
         * <p>This method can only be used to measure elapsed time and is
         * not related to any other notion of system or wall-clock time.
         * The value returned represents nanoseconds since some fixed but
         * arbitrary time (perhaps in the future, so values may be
         * negative).  This method provides nanosecond precision, but not
         * necessarily nanosecond accuracy. No guarantees are made about
         * how frequently values change. Differences in successive calls
         * that span greater than approximately 292 years (2<sup>63</sup>
         * nanoseconds) will not accurately compute elapsed time due to
         * numerical overflow.
         *
         * <p> For example, to measure how long some code takes to execute:
         * <pre>
         *   long startTime = System.nanoTime();
         *   // ... the code being measured ...
         *   long estimatedTime = System.nanoTime() - startTime;
         * </pre>
         *
         * @return The current value of the system timer, in nanoseconds.
         * @since 1.5
         */
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    I'm back , sorry for make you wait so long
    the results change , with a = 44444 and b = 12 nanotime is 1836
    , while changing values with each other ntime is 709484.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Have you solved you problem?
    Or do you still have questions?

  8. #8
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    no , I get an aswer to my question, i' ve nothing else to ask , i posted just for this
    Come back and post the results here.
    bye

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  2. New to Java Program
    By jvasilj1 in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 08:22 AM
  3. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  4. help with java program
    By mattvgt in forum New To Java
    Replies: 3
    Last Post: 07-14-2007, 05:57 PM
  5. java program
    By nehasahu in forum New To Java
    Replies: 5
    Last Post: 07-12-2007, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •