Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: looping through a big number quickly

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default looping through a big number quickly

    I've thought about this for a bit and when you're using a for loop and you for some reason have to loop through a number like:

    100000000000000000000000000000000000000000000. ( idk why you would have to, but bear with me ) that takes ages with a for loop.

    now i thought about maybe splitting it up in some threads to all add up to the same variable, what would you do?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,575
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: looping through a big number quickly

    If the steps being executed inside the loop can be done in parallel in separate threads and the computer the code is executing on has multiple processors so each thread can run on its own processor, then the total elapsed time to do the whole task could be shortened by splitting the work over the threads.
    Using more threads than there are processors can cause contention and lengthen the time.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    617
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: looping through a big number quickly

    Depends on what you want to do. If each iteration must only start after all previous iterations have already finished then you can not use several threads.
    On a single core you cant get much faster then a for-loop.

Similar Threads

  1. Replies: 3
    Last Post: January 25th, 2013, 05:53 AM
  2. quickly please
    By fatema in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2013, 09:53 AM
  3. I need help quickly
    By dan123 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2012, 04:58 PM
  4. need help quickly plz!!!
    By computerfreak.7777 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 11th, 2010, 01:32 AM