Thread: Comparing execution times

  1. #16
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    The only problem I am having then is fixing up my for loop. I can't figure out how to set it up based off of the feedback I have received. I'm trying to think of ways t rework it, but I can't seem to figure it out.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Come on. How would you do it in real life (n=2)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    Are you saying that I should be assigning, in my case, i the value of 2 in the for loop? Reading
    Code:
     for (int i = 2; i <= num; i++)
    . Or am I completely missing what you are trying to say?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read through that loop. Assuming I want to compute n(n+1)/2 for n=2 (hence, I input num=2). What does the loop do and what is the result?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    The result of the loop will be 3, and that is also what i am getting as my answer

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that's interesting, because if I take your original program and change your for loop with the loop you've written above, I get the answer 2 when inputting 2, while I should be getting 3.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    Ooops, sorry, I was referring back to my original loop that I had first written. You are correct about the loop that I had just given.

  8. #23
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    I am trying to work on the closed form approach, which from what I have found you are just assigning the formula to the variable. For my code, I have,
    Code:
     start = clock(); int formula = (n*(n+1))/2; end = clock();
    but when I try and enter in a value like 10^7 I get 0 seconds. That is also the same when I try 10^10 as the value for n. Shouldn't I actually get a time or no?
    Last edited by newbieprogram; 03-21-2014 at 12:03 PM.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's too fast. You need to repeat it in a loop a number of times.
    Your formula takes around 5 instructions, which on a 2 GHz processor is about 10 nanoseconds, excluding time for reading and writing from memory/cache.
    Last edited by Elysia; 03-21-2014 at 12:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    If I repeat it in a loop, is that still using the closed form approach?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the closed form approach is using the formula. You can, of course, repeat this formula many times. It's still the same formula, hence the closed form approach.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    Ok, after putting the closed form approach inside of a for loop, i am now getting a result. I really do appreciate all of the help you have provided. Sorry if I was being a pain or anything like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing time taken for execution
    By AvaGodess in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:15 PM
  2. comparing times
    By cnu_sree in forum C Programming
    Replies: 13
    Last Post: 06-08-2007, 01:15 AM
  3. Outputing 2 times?
    By Tropicalia in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2006, 09:21 PM
  4. Times.h
    By LearningC in forum C Programming
    Replies: 6
    Last Post: 10-02-2005, 09:09 AM

Tags for this Thread