Thread overview
scanf vs std.stream.stdin.scanf - scanf.d.txt
Feb 12, 2005
g.wiener
Feb 12, 2005
Ben Hinkle
Feb 13, 2005
Gerry Wiener
Feb 13, 2005
Ben Hinkle
February 12, 2005
I just downloaded the Linux version of D on Jan 31, 2005.

I've run a simple test comparing the performance of the C version of scanf (in D) versus the std.stream.stdin.scanf performance.

On my Debian linux box, the D version of std.stream.stdin.scanf is 10 times slower on a file consisting of 768000 numbers than the C version.

I'm wondering why there is such a drastic difference in processing speed.

Thanks,

Gerry Wiener



extern(C)
{
int  scanf	(char *, double *);
}

int main()
{
double sum = 0;
double value;

while (scanf("%lg", &value)>= 0)
{
sum += value;
}

printf("sum = %g\n", sum);
return(0);
}


--------------------------------------------------------------------


import std.stream;

int main()
{
double sum = 0;
double value;

while (std.stream.stdin.scanf("%lg", &value)>= 1)
{
sum += value;
}

printf("sum = %g\n", sum);
return(0);
}


February 12, 2005
Most likely you are seeing the fact that std.stream.stdin is not buffered.
If you replace the line
while (std.stream.stdin.scanf("%lg", &value)>= 1)
with
BufferedStream bufferedin = new BufferedStream(stdin);
while (bufferedin.scanf("%lg", &value)>= 1)
then on WinXP where I tested it goes from about 7 times slower than the C
version to 2 times faster than the C version.

<g.wiener@comcast.net> wrote in message news:culubm$2mbl$1@digitaldaemon.com...
>I just downloaded the Linux version of D on Jan 31, 2005.
>
> I've run a simple test comparing the performance of the C version of scanf
> (in
> D) versus the std.stream.stdin.scanf performance.
>
> On my Debian linux box, the D version of std.stream.stdin.scanf is 10
> times
> slower on a file consisting of 768000 numbers than the C version.
>
> I'm wondering why there is such a drastic difference in processing speed.
>
> Thanks,
>
> Gerry Wiener
>
>
>
> extern(C)
> {
> int  scanf (char *, double *);
> }
>
> int main()
> {
> double sum = 0;
> double value;
>
> while (scanf("%lg", &value)>= 0)
> {
> sum += value;
> }
>
> printf("sum = %g\n", sum);
> return(0);
> }
>
>
> --------------------------------------------------------------------
>
>
> import std.stream;
>
> int main()
> {
> double sum = 0;
> double value;
>
> while (std.stream.stdin.scanf("%lg", &value)>= 1)
> {
> sum += value;
> }
>
> printf("sum = %g\n", sum);
> return(0);
> }
>
>
> 


February 13, 2005
Thanks Ben -- that's it! Now I'm seeing D outperform gcc by about 10% on this example.

--Gerry

In article <cum1jh$2pas$1@digitaldaemon.com>, Ben Hinkle says...
>
>Most likely you are seeing the fact that std.stream.stdin is not buffered.
>If you replace the line
>while (std.stream.stdin.scanf("%lg", &value)>= 1)
>with
>BufferedStream bufferedin = new BufferedStream(stdin);
>while (bufferedin.scanf("%lg", &value)>= 1)
>then on WinXP where I tested it goes from about 7 times slower than the C
>version to 2 times faster than the C version.
>
><g.wiener@comcast.net> wrote in message news:culubm$2mbl$1@digitaldaemon.com...
>>I just downloaded the Linux version of D on Jan 31, 2005.
>>
>> I've run a simple test comparing the performance of the C version of scanf
>> (in
>> D) versus the std.stream.stdin.scanf performance.
>>
>> On my Debian linux box, the D version of std.stream.stdin.scanf is 10
>> times
>> slower on a file consisting of 768000 numbers than the C version.
>>
>> I'm wondering why there is such a drastic difference in processing speed.
>>
>> Thanks,
>>
>> Gerry Wiener
>>
>>
>>
>> extern(C)
>> {
>> int  scanf (char *, double *);
>> }
>>
>> int main()
>> {
>> double sum = 0;
>> double value;
>>
>> while (scanf("%lg", &value)>= 0)
>> {
>> sum += value;
>> }
>>
>> printf("sum = %g\n", sum);
>> return(0);
>> }
>>
>>
>> --------------------------------------------------------------------
>>
>>
>> import std.stream;
>>
>> int main()
>> {
>> double sum = 0;
>> double value;
>>
>> while (std.stream.stdin.scanf("%lg", &value)>= 1)
>> {
>> sum += value;
>> }
>>
>> printf("sum = %g\n", sum);
>> return(0);
>> }
>>
>>
>> 
>
>


February 13, 2005
"Gerry Wiener" <Gerry_member@pathlink.com> wrote in message news:cumc3n$6u$1@digitaldaemon.com...
> Thanks Ben -- that's it! Now I'm seeing D outperform gcc by about 10% on
> this
> example.
>
> --Gerry

It would be interesting to find out what the buffer size is for gcc. Maybe
it really is worth making the stdio stream buffered by default. The lines in
question are way at the bottom of std.stream it says things like
      stdin = new File(0, FileMode.In);
and all we would need to do if we decide to make them buffered is
      stdin = new BufferedFile(0, FileMode.In);

-Ben