Thread: Some what interesting comparison

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Some what interesting comparison

    When writing my paper concerning Pascal and C I noticed the following:

    For the "Hello World" program the Pascal executeable was 42484 bytes and the C executeable was 4822 bytes.
    Using the time command the real was .003s for each of the 5 invocations of the C executeable but was .001s for each of the 5 invocations of the Pascal executeable.

    C code:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
      printf("Hello World\n");
      return 0;
    }
    Pascal code:
    Code:
    begin
      writeln('Hello World');
    end.
    The other program was showing how the subroutine definations are(or can be) arranged in C and Pascal.
    Pascal Executable 46212, C Executable 5012.
    time: C: .003s for each of 5 invocations. Pascal: .001 for 4 of the 5 invocations, .002 for the 5th.

    C code
    Code:
    #include <stdio.h>
    void p1 (int);
    void p2 (int);
    void p3 (int);
    
    int main (void)
    {
      int x;
      x = 5;
      printf("%d ",x);
      p1(x);
      return 0;
    
    }
    
    void p3 (int x)
    {
      printf("%d\n", x*x);
    }
    
    void p1 (int x)
    {
      x = x + 1;
      printf("%d ", x);
      p2(x);
    }
    
    void p2 (int x)
    {
      x = x * 2;
      printf("%d ", x);
      p3(x);
    }
    Pascal code
    Code:
    procedure p3 ( x : integer);
      begin
        writeln(x*x)
      end;
    
    procedure p2 ( x : integer);
      begin
        x := x * 2;
        write(x, ' ');
        p3(x)
      end;
    
    procedure p1 ( x : integer);
      begin
        x := x + 1;
        write (x, ' ');
        p2(x)
      end;
    
    var
      x : integer;
    begin
      x := 5;
      write (x, ' ');
      p1(x)
    end.
    While I'm sure most of you don't care I was suprised on the size of the exectuable and the speed of writting to the screen.

    Enviroment: Debian, PII 667 Mhz
    Pascal Compiler: Free Pascal Compiler version 1.0.4 [2001/08/31] for i386
    C Compiler: gcc version 2.95.4 20011002 (Debian prerelease)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try comparing when you use puts() for your constant strings instead of printf(). printf() has to parse your string one char at a time looking for % conversions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    with puts() in the hello world it ran in all 5 times in .002 seconds. Executable size was 4816 bytes.

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    another thing to check for is whether both or neither are outputting optimized code. That could throw your results if one is and the other isn't.
    PHP and XML
    Let's talk about SAX

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Both were unoptimized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. A few interesting tools...
    By Mad_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-10-2009, 06:07 PM
  4. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM
  5. very interesting.....VERY interesting
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-16-2002, 10:02 PM