Page 1 of 1

How fast is Pascal?

Posted: Wed Nov 06, 2002 12:16 pm
by lowai
I wrote some codes in Pascal, and they got TLE.
Then I rewrote them in C++, and got AC.

Posted: Wed Nov 06, 2002 5:09 pm
by zhangl
webmaster please come here.

Posted: Fri Nov 08, 2002 6:38 pm
by fpnc
My answer to something quite similar to your question:
I'm writing about Online Judge. I noticed from the server status page, http://acm.uva.es/cgi-bin/OnlineJudge?Status:Valladolid that, unless there's runtime error, all code run either under 1 second or over 10 seconds. There might be something wrong with how the judge measure the time usage.

The runtime of one of my code jumps from 10.1 seconds to 0.98 seconds just by a small optimization.
I'm pretty sure our system is OK. I've seen the stats right now, and there is a plenty of <0.1sec and >=10sec, but there are also 1.307 (1224016), 2.920 (1223965), 6.969 (1223960) and so on.

The reason for this is simple. Until some days we gave 30 secs per program. Now we only give 10 secs except particular problems. This means that all the sources that took >10sec and <30sec now get TL.
Also, our inputs and outputs are usually quite limited, so most programs either solve it quickly (remember that our system is PIII 1.5GHz [ error: it is actually 800MHz]) or they fall into infinite loops (mainly because reading the input wrongly). I really don't think that our system is broken.

BTW, we need help producing input and outputs. If you are able to, you can produce a really big input/output so programs take more time to solve and the ranking will be more accurated.

FYI, we measure the time using wait3() function. That small optimization maybe wasn't so small after all...
NOTE: if you don't see more than 10.x secs this is because TL is now 10sec for most problems, and we stop running programs as soon as they reach 10secs and we detect that situation (so it is not 10.000 secs exactly).

Posted: Fri Nov 08, 2002 6:43 pm
by fpnc
For an example, you can see that problem 452 has been solved in C, C++ and Pascal, and it takes little more time to run (I'm sure my algorithm is not the best, anyway).

But I don't have a way to answer you definitely. I just can suggest you to download freepascal and try it by yourself. If your C++ code took more than 70% of allowed time to run (7secs for most problems), then I would think that is to blame. But if it doesn't I would say that something changed when porting from Pascal to C++...

Posted: Fri Nov 08, 2002 8:53 pm
by Ivan Golubev
To fpnc
Hmm, is it real that judge system is PIII 1.5GHz? Judge status page shows ~1600 bogomips and this looks like that OJ system still running at PIII-800 processor...
Also, check out ranklist for P10399, there is a running time 1.1000 (which seems to be 2 seconds). Don't you think that it's a bit strange? ;-)

To lowai
What problem you've recoded from Pascal to C? I don't have FreePascal but suspect one possible reason -- GNUC even with no optimizations turned on changes division by constant to sequence of multiply/adding/substracting/shifting which is much faster than "true" division operation. I not sure that Pascal compiler does the same thing, so if you use in large cycles constructions like:[pascal]y := x div 10; x := x mod 10;[/pascal](for example, it can be in P10013 "Super long sums") then C construction [c]y = x / 10; x = x % 10[/c] will be compiled into much faster machine code. May be somebody who has GCC & FreePascal can check this out?..

Running times

Posted: Fri Nov 08, 2002 10:18 pm
by abiczo
I have both GCC (2.95.4) and Freepascal (1.0.6), so I tested what Ivan wrote.

I tried the following programs:
[pascal]
program test;
var i, x, y: longint;
begin
for i:=1 to 10000000 do
begin
x:=i;
y:=x mod 10; x:=x mod 10;
end;
end.
[/pascal]
[c]
int main()
{
int i, x, y;
for(i=1;i<=10000000;i++) {
x=i;
y=x/10; x=x%10;
}
return 0;
}
[/c]

I compiled the programs without any additional arguments (gcc test.c, ppc386 test.p).
The C program run for 0.540 sec and the Pascal program run for 1.650 sec on my 500mhz machine.
(I used linux's time command to measure running times.)[/c][/pascal]

Posted: Fri Nov 08, 2002 11:59 pm
by fpnc
Ivan Golubev wrote:To fpnc
Hmm, is it real that judge system is PIII 1.5GHz? Judge status page shows ~1600 bogomips and this looks like that OJ system still running at PIII-800 processor...
Ooops :oops: My fault here. P3-800 of course!:

Memory: 1027688k/1048496k available (1153k kernel code, 17228k reserved, 975k data, 160k init, 130992k highmem)
Detected 803.362 MHz processor.
Ivan Golubev wrote:Also, check out ranklist for P10399, there is a running time 1.1000 (which seems to be 2 seconds). Don't you think that it's a bit strange? ;-)
I'll remark again that we use wait3() to get info about processes... I don't know what more can I do about this matter...

Posted: Mon Nov 15, 2004 9:32 pm
by randomtaiwanese
everything is faster than Java... NO REALLY

there are some problems out there that is impossible to solve with JAVA!!!

because it is sooo slow and the OJ does not scale the time (or scales not enough)

this is what i tried with the OJ
Problem 136 Ugly Numbers, a simple one-liner, should take no time to run...
http://acm.uva.es/cgi-bin/OnlineJudge?S ... 999::50276

JAVA CPU TIME: 0.004
[java]class Main
{
public static void main(String[] args)
{
System.out.println("The 1500'th ugly number is *********.");
}
}[/java]

PASCAL CPU TIME: 0.002
[pascal] Program UglyNumbers_136;
begin
writeln('The 1500''th ugly number is *********.');
readln();
end.[/pascal]

PASCAL CPU TIME: 0.000
[pascal] Program P1;
begin
writeln('The 1500''th ugly number is *********.');
end.[/pascal]

C++ CPU TIME: 0.000
[cpp]#include <stdio.h>

int main()
{
printf("The 1500'th ugly number is *********.\n");
return 0;
}[/cpp]