Compile time of C versus C++ code

This is a discussion on Compile time of C versus C++ code within the C++ Programming forums, part of the General Programming Boards category; Ah well... I think I might know why. As you know, it uses memcpy to copy the object since you ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Ah well... I think I might know why.
    As you know, it uses memcpy to copy the object since you have to create a local object to modify due to the class not having an operator + (double val) overloaded operator. Allocating on the heap means it doesn't have to copy the array, I think.
    That's the most logical explanation I can find. And it seems it can actually also optimize the memcpy call.
    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.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, thats the entire reason - the matrix object itself is no longer 10000 bytes (50 * 50 * 4), but about 16 bytes, so we don't need to copy lots and lots of data. It's cheaper to call new than it is to copy many bytes of data.

    Of course, if the matrix actually needs to be 50 x 50 elements, rather than the 3 x 3 that is used in this example, it would be quite a different story.

    Solving this using a templated matrix type that uses the two sizes as input would probably work best.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    So mats, what's your current code?
    It's not playing nice with me. Just a few changes and it takes 72 seconds. Bah.
    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.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can post the current code, but be aware that it's a hack - I have just changed it enough to prove a point, no memory freeing, and it's probably "broken" in several aspects of "copy & assignment constructors". I only checked that the result is OK for THIS sample.

    I've attached the source file - imaginatively named, I know.

    --
    Mats
    Attached Files Attached Files
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    You seem to be initializing the variables in the function. And to 3x3, no less, which is far cry from the 50x50 the original used.

    matrix k1(3, 3), k2(3, 3), k3(3, 3), k4(3, 3), k5(3, 3), k6(3, 3), k(3,3);

    Plus k5 and k6 are unreferenced.
    But what confuses me most is why

    k1=dt*(linv*u-linv*r*i);
    k2=dt*(linv*u-linv*r*(i+(1.0f/2.0f)*k1));
    k3=dt*(linv*u-linv*r*(i+(1.0f/2.0f)*k2));
    k4=dt*(linv*u-linv*r*(i+k3));
    k=(k1+2.0*k2+2.0*k3+k4)*(1.0f/6.0f);

    Works for you... but doesn't for me.
    I simply get

    error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const matrix' (or there is no acceptable conversion)

    I'm not sure if you've done something to fix that?
    Last edited by Elysia; 02-06-2008 at 06:27 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.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by matsp View Post
    Probably not a good idea. gcc has a pretty good handle on when to inline and when not in itself.
    It can only choose when NOT to inline. It will only inline if you tell it.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 10:36 AM
  2. Can you create, edit and compile C code with a C++ IDE?
    By nerdpirate in forum C Programming
    Replies: 4
    Last Post: 05-31-2008, 02:54 AM
  3. Replies: 0
    Last Post: 11-29-2002, 10:24 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21