Thread: Optimization question

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Alright thanks! That is good news.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    template<int Num1, int Num2> struct divide
    {
    	static const int value = Num1 / Num2;
    };
    
    int main()
    {
    	std::cout << divide<10, 5>::value << std::endl;
    }
    No computation of division at runtime
    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
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Code:
    #include <iostream>
    *
    template<int Num1, int Num2> struct divide
    {
    	static const int value = Num1 / Num2;
    };
    
    int main()
    {
    	std::cout << divide<10, 5>::value << std::endl;
    }
    No computation of division at runtime
    The only compiles that are too retarded to optimize out "10 / 5" are unlikely to be able to handle these advanced template constructs, I think. So I think it would be really bad to use a messy construct like this.
    In fact, I find it more likely a compiler is unable to optimize something like divide<10, 5>*3 than it 10 / 5 * 3...

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No one would write that, he's showing off

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point was that it will be optimized even in Debug mode. In Release, I'm sure the compiler will optimize it.
    And any compiler that is able to instantiate a template like this will have no problems optimizing divide<10, 5> * 3.
    And some might write like this, if not for exactly division, but for other things such as binary -> decimal or whatever.
    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.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    And some might write like this, if not for exactly division, but for other things such as binary -> decimal or whatever.
    Apples and oranges...

    Specific to your example, you can't do a/b for any non-constant expressions a or b, something you should have really mentioned maybe. So yeah you're showing off, no one would write that.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler can't optimize non-constant expressions to the results anyway, even without the template, so I figure it is kind of the same
    No constant expressions = no optimization (unless it's SSE or the like).
    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.

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How do you know the compiler doesn't already evaluate constant expressions in debug build? E.g with gcc:

    Code:
    int a = 14 / 2;
    Code:
    .stabn 68,0,5,LM3-_main
    LM3:
    	movl	$7, -4(%ebp)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, as I said, with optimizations on (ie Release build), then it 99% likely will.
    But in Debug mode (ie no optimizations), it most likely will not (VC++ will not).
    And if they aren't constant expressions, then neither will work very well (unless we talk about SSE & Co).
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Then why optimize arithmetic in debug builds at all?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because:
    Quote Originally Posted by iMalc View Post
    Option B is definitely better though because it almost certainly increases the speed of your debug builds, which can be a godsend.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  4. Optimization Question
    By saxman in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 10:49 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM