Thread: hard problem

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    hard problem

    can someone please show me a simple for loop that is so slow (exponential?) it takes a minute to run?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Code:
    for (;;) {  }

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    1. create a function that increments a static variable or otherwise does some work that can be discarded
    2. make a nested loop with multiple levels where the inner loop calls the function . start with two loops, one inside the other
    3. tune the loop counts until it runs as long as you want. start with INNER = 1 and OUTER = something kind of big, like 1000000
    4. run it. double the value of OUTER until it runs 10 seconds
    5. change INNER to 6
    6. if you can't get it to run 10 seconds with INNER = 1, double INNER and start over with OUTER

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What rolls down stairs alone or in pairs
    Rolls over your neighbor's dog?
    What's great for a snack and fits on your back?
    It's Log, Log, Log!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well, a loop from 1 to 60 which consisted of entirely a Sleep for 1 second would do it.

    You're going to have to do better at asking questions to get what you are really after!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by studus View Post
    can someone please show me a simple for loop that is so slow (exponential?) it takes a minute to run?
    A minute running on what kind of hardware? Ever try to play an old DOS game on new hardware and find that timing events happen ridiculously fast, because they were written for a 25 MHz processor?

    Of course, the better question isn't how to do this - it's why WOULD you do this? Sleep functions have been standard for decades, and pre-emptively multitasking operating systems are going to strongly encourage you to use them, so that other processes and threads can run on the core that you would otherwise be monopolizing. MacGuyvering your own poor substitute for sleep() isn't just unnecessary, it's not going to work as well.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c code requested- really hard problem
    By amnakhan786 in forum C Programming
    Replies: 4
    Last Post: 11-13-2011, 03:21 AM
  2. Another hard C problem
    By alexbcg in forum C Programming
    Replies: 12
    Last Post: 11-23-2010, 08:03 AM
  3. adding strings hard problem...
    By qubit67 in forum C Programming
    Replies: 28
    Last Post: 04-22-2007, 02:02 AM
  4. Hard Drive Problem
    By MethodMan in forum Tech Board
    Replies: 3
    Last Post: 10-08-2002, 05:13 PM
  5. Hard Problem :(
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2002, 03:16 AM