Efficient Coding Styles

This is a discussion on Efficient Coding Styles within the C Programming forums, part of the General Programming Boards category; Hey all been thinking about this a bit reciently. which of the following would run faster?? ------------------------------------------------ printf("Hello World"); ----------------------------------------------- ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Question Efficient Coding Styles

    Hey all

    been thinking about this a bit reciently.
    which of the following would run faster??
    ------------------------------------------------
    printf("Hello World");
    -----------------------------------------------

    -----------------------------------------------
    char * string = "Hello World";
    printf("%s", string);
    -----------------------------------------------
    or
    -----------------------------------------------
    printf("%s","Hello World");
    -----------------------------------------------

    I dont particularly want to know for the printf function, but functions in general.. (printf is prob a bad example)

    I sometimes find it easier to read the code if it uses the 2nd style..
    I figure that once the code is compiled it wouldnt make a difference.

    Ive heard that the second style can make it easier for the compiler to compile code, which means the executable would be that little bit faster, Especially if your sending the return value from a function directly into that function:
    .. hmm not to good at explainging so heres an example

    ----------------------------------------------------------------------
    func2(func1());
    ----------------------------------------------------------------------

    does anyone have any input?

    cheers,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    First program's c code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello World!\n");
        return 0;
    }
    compiled with -O3 to assembly
    Code:
            .file   "tmp.c"
            .version        "01.01"
    gcc2_compiled.:
    .section        .rodata
    .LC0:
            .string "Hello World!\n"
    .text
            .align 4
    .globl main
            .type    main,@function
    main:
            pushl %ebp
            movl %esp,%ebp
            pushl $.LC0
            call printf
            xorl %eax,%eax
            leave
            ret
    .Lfe1:
            .size    main,.Lfe1-main
            .ident  "GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"
    Second program's c code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char* s = "Hello World!\n";
        printf(s);
        return 0;
    }
    Compiled with gcc -O3
    Code:
           .file   "tmp1.c"
            .version        "01.01"
    gcc2_compiled.:
    .section        .rodata
    .LC0:
            .string "Hello World!\n"
    .text
            .align 4
    .globl main
            .type    main,@function
    main:
            pushl %ebp
            movl %esp,%ebp
            pushl $.LC0
            call printf
            xorl %eax,%eax
            leave
            ret
    .Lfe1:
            .size    main,.Lfe1-main
            .ident  "GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"
    And surprisingly it is the same assembly output.

  3. #3
    Unregistered
    Guest
    Agree above,hehe.
    I come from china ,and want to learn more.
    I feel it is a useful place, I will come here more often.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Thanks

    hey,
    I figured it'd be something like that..
    you wouldnt be able to tell me more about compiling to assembly would ya.. it looks interesing.

    thanks again,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 10:17 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 01:26 AM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

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