Bisma Jayadi
2006-06-12 11:09:42 UTC
Actually, C isn't faster. That's a Myth (tm)
Is there any scientific proof of this and written by a really known good c orpascal programmer? Any URLS? I just can't say it without a real acceptable
proof. Even the language-shoutout website still shows c (gcc) is faster than fpc. :(
Though I'm a big fan of object pascal but I found myself that c (gcc) IS faster
than fpc. Here's the simple comparison example:
#include <stdio.h>
int main()
{
int num, res;
char buf[4*1024];
do {
res = scanf("%d", &num);
if (res==EOF || num==0) break;
printf("%dn", num);
} while (1);
}
This c (gcc) program run on 0.303 second. The closest equal pascal version I
think is...
program Test;
var
I: Integer;
begin
repeat
Readln(I);
if I = 0 then Break;
Writeln(I);
until False;
end.
This fpc program run on 1.772 second (about 6 times slower than the c version). :(
A little optimization below still can't beat gcc ...
program Test;
uses
Dos;
const
MaksBuf = 4 * 1024;
var
I: Integer;
BufBaca, BufTulis: array[0..MaksBuf-1] of Char;
begin
SetTextBuf(Input, BufBaca, MaksBuf);
SetTextBuf(Output, BufTulis, MaksBuf);
with TextRec(Output) do
FlushFunc := nil;
repeat
Readln(I);
if I = 0 then Break;
Writeln(I);
until False;
Flush(Output);
end.
And the result run on 0.407 second, but with some additional optimization codes.
I can make a same output result using FPC that can be faster than GCC, but with
LOTS of more additional optimization codes (I need to "take over" the buffer
read and write operation). So, I don't think it's counted. :D
When the same second code above is compiled using Kylix (v.3), it run on 1.235
second. So, obviously FPC is a lot better than Kylix. :)
Tested using GCC 4.0 (with -O2 param) and FPC 2.0.0 (with -OG param) on Linux
(kernel v2.6.12 dan glibc v2.3.5) on P4 2.8 GHz with 2 GB ram, to read a text
file contains 500.000 lines of random numbers (integers). Utilizing "time"
command for time measurements (bash$ time ./test < input.txt > output.txt).
Any hints? :)
PS: I don't mean to start a flame here. Just to show you the simple fact. I just
want to show the world that the "c is faster than pascal" is REALLY a Myth(tm).
And remember that I'm a proud user of FPC/Lazarus, so I'm not the enemy. :)
-Bee-
has Bee.ography at:
http://beeography.wordpress.com