Page 1 of 1

why so much mem?

Posted: Wed Sep 25, 2002 9:35 am
by Fernandez Jose
how it's possible that this code spend 320kb of memory?
program factor;
const
F7=string('5040');
F8=string('40320');
F9=string('362880');
F10=string('3628800');
F11=string('39916800');
F12=string('479001600');
F13=string('6227020800');
sOver=('Overflow!');
sUndr=('Underflow!');

var
dato:longInt;
begin
readln(dato);
while (not eof(input)) do
begin
readln(dato);
if dato<0 then begin
if dato mod 2 = 0 then writeln(sUndr) else writeLn(sOver) end
else begin
case dato of
0..6: writeLn(sUndr);
7: writeLn(F7);
8: writeLn(F8);
9: writeLn(F9);
10: writeLn(F10);
11: writeLn(F11);
12: writeLn(F12);
13: writeLn(F13);
else writeLn(sOver);
end; end;
end;
end.

I really don't understand that a 36 lines program with a single variable can be so memory greedy

Posted: Wed Sep 25, 2002 11:13 am
by Ivor
As far as I have figured it out, it has to do something with internal IO buffering. Not sure about it though.

Ivor

Posted: Wed Sep 25, 2002 1:22 pm
by Fernandez Jose
Probably you'r right. But as anyhow I realize that the same algorithm in C++ is much faster than in Pascal. The problem 264 (Count on Cantor) in Pascal uses 324kb of memory and 0.130 s. of CPU time, while in C++ the figures are 64kb for memory and 0.010 s. of time.
All that despite I don't know C or C++, appart from the basics.

Posted: Wed Sep 25, 2002 2:20 pm
by Ivor
Are you sure the algorithm you used is exactly the same? :roll:

I have noticed that Pascals IO is too slow -- C, C++ can almost always beat it. But I have also noticed that pascals floating point functions are slighly faster that that of C. That was true for gnu pascal, don't know about freepascal. I just noticed it when I was looking through ranklists.

Ivor