--tKW2IUtsqtDRztdT
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Aug 21, 2006 at 03:55:10PM +0900, MonkeeSage / gmail.com wrote:
> Ok, I need to preface this by saying that I'm in no way either a C or
> ruby guru, so I may have missed something here, but this is what I'm
> seeing. The bottle-neck here is in the printing to screen.
>=20
> I don't see how the OP got the code to run in 5 seconds while using any
> stdout writing function (e.g., printf). The program should print like 4
> megs of text to the screen (though I couldn't get the C that was posted
> to compile--like I said, I'm no C superstar) -- there's no way that is
> happening in 5 seconds.

First, stdout doesn't neccesarily write to your screen, and /dev/null
is very very fast. Even if writing to some kind of terminal emulator,
some of them are much faster with bulk output than others.

>=20
> Taking a very simple test case, which just prints a 10 byte char array
> 15000 times using puts:
>=20
> --------
>=20
> #include <stdio.h>
> #define SIZE 15000
> int main() {
>   char str[11]   =3D "ABCDEFGHIJ";
>   int i;
>   for (i =3D 0; i < SIZE; ++i) {
>     puts(str);
>   }
>   printf("Printed %i bytes of data\n\0", SIZE*strlen(str));
>   return 0;
> }
>=20
> --------
>=20
> Compiled with:
>=20
> gcc -o test test.c
>=20
> This yields:
>=20
> time ./test
>=20
> ABCDEFGHIJ
> ABCDEFGHIJ
> ...
> ABCDEFGHIJ
> Printed 150000 bytes of data
>=20
> real    0m25.621s
> user    0m0.010s
> sys     0m0.029s
>=20
>=20
> Now, let's see how Ruby's stdout writing stacks up:
>=20
> --------
>=20
> #!/usr/bin/ruby -w
> SIZE =3D 15000
> str =3D "ABCDEFGHIJ"
> 1.upto(SIZE) {
>   STDOUT.syswrite("#{str}\n")
> }
> STDOUT.syswrite("Printed #{SIZE*str.length} bytes of data\n")
>=20
> --------
>=20
> This yields:
>=20
> ABCDEFGHIJ
> ABCDEFGHIJ
> ...
> ABCDEFGHIJ
> Printed 150000 bytes of data
>=20
> real    0m26.796s
> user    0m0.202s
> sys     0m0.049s
>=20
> Pretty comparable there.
>=20
>=20
> Of course, the bottle-neck was *supposedly* the maths and array access
> and such, which is where C would excel (I'm not denying that ruby is
> sometimes pretty slow, or that C is *much* faster all around, just bare
> with me here).

Not so. Let's look at the *user* numbers which represent CPU time used
by the programs themselves. The difference between 0.010 and 0.202s is
pretty large (2000%), but maybe the values are still too small to
disregard startup time etc.

99.999% of the *real* execution time your program was not even running,
but waiting, presumably for a slow output system to write your strings.

All you did benchmark was that your terminal emulator (or whereever
your stdout goes to) is slow.

I suggest to increase SIZE and redirect stdout to the bitbucket (or a
file if you lack a decent terminal) and want to compare only the C and
ruby versions again. The difference will be much more visible even in
real execution times then.

J=FCrgen

--=20
 The box said it requires Windows 95 or better so I installed Linux

--tKW2IUtsqtDRztdT
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iQEVAwUBROucufy64gyiEfXtAQIXyAf+PGMyYBuPi+FIZ2sLYOgGNL8lfs5gJ/97
1lvmbhU17DVZ6+QfLCGslchyDNHLK4EgXsZB1LXVixMVDbaEGvic2IqV4eJ1D2B5
jnER7QkNhkaCGTx7T7pKNM2LCP7dFaVfzHl4dqYnlwi9ZZybYTPx9XynY0sH+pBM
I2xNzxLjz6KHJYBZyC2+qcVg4CbLwBuC8z+moYHkw1IXXYqAhPIKBdPSiPLh/kzK
Zyi25Y29V/CWgIqGMEeM4PrzBieXIRs0qg7/xzDrzCt28nV2SIJAySQdk/Asp9qD
6BBqMCNJDfHZ+fmeorUouf6eAxhf+W1H7e2U+a1oHh/ZrjRT5dD2OA==
=Gfk/
-----END PGP SIGNATURE-----

--tKW2IUtsqtDRztdT--

On Mon, Aug 21, 2006 at 03:55:10PM +0900, MonkeeSage / gmail.com wrote:
> Ok, I need to preface this by saying that I'm in no way either a C or
> ruby guru, so I may have missed something here, but this is what I'm
> seeing. The bottle-neck here is in the printing to screen.
>=20
> I don't see how the OP got the code to run in 5 seconds while using any
> stdout writing function (e.g., printf). The program should print like 4
> megs of text to the screen (though I couldn't get the C that was posted
> to compile--like I said, I'm no C superstar) -- there's no way that is
> happening in 5 seconds.

First, stdout doesn't neccesarily write to your screen, and /dev/null
is very very fast. Even if writing to some kind of terminal emulator,
some of them are much faster with bulk output than others.

>=20
> Taking a very simple test case, which just prints a 10 byte char array
> 15000 times using puts:
>=20
> --------
>=20
> #include <stdio.h>
> #define SIZE 15000
> int main() {
>   char str[11]   =3D "ABCDEFGHIJ";
>   int i;
>   for (i =3D 0; i < SIZE; ++i) {
>     puts(str);
>   }
>   printf("Printed %i bytes of data\n\0", SIZE*strlen(str));
>   return 0;
> }
>=20
> --------
>=20
> Compiled with:
>=20
> gcc -o test test.c
>=20
> This yields:
>=20
> time ./test
>=20
> ABCDEFGHIJ
> ABCDEFGHIJ
> ...
> ABCDEFGHIJ
> Printed 150000 bytes of data
>=20
> real    0m25.621s
> user    0m0.010s
> sys     0m0.029s
>=20
>=20
> Now, let's see how Ruby's stdout writing stacks up:
>=20
> --------
>=20
> #!/usr/bin/ruby -w
> SIZE =3D 15000
> str =3D "ABCDEFGHIJ"
> 1.upto(SIZE) {
>   STDOUT.syswrite("#{str}\n")
> }
> STDOUT.syswrite("Printed #{SIZE*str.length} bytes of data\n")
>=20
> --------
>=20
> This yields:
>=20
> ABCDEFGHIJ
> ABCDEFGHIJ
> ...
> ABCDEFGHIJ
> Printed 150000 bytes of data
>=20
> real    0m26.796s
> user    0m0.202s
> sys     0m0.049s
>=20
> Pretty comparable there.
>=20
>=20
> Of course, the bottle-neck was *supposedly* the maths and array access
> and such, which is where C would excel (I'm not denying that ruby is
> sometimes pretty slow, or that C is *much* faster all around, just bare
> with me here).

Not so. Let's look at the *user* numbers which represent CPU time used
by the programs themselves. The difference between 0.010 and 0.202s is
pretty large (2000%), but maybe the values are still too small to
disregard startup time etc.

99.999% of the *real* execution time your program was not even running,
but waiting, presumably for a slow output system to write your strings.

All you did benchmark was that your terminal emulator (or whereever
your stdout goes to) is slow.

I suggest to increase SIZE and redirect stdout to the bitbucket (or a
file if you lack a decent terminal) and want to compare only the C and
ruby versions again. The difference will be much more visible even in
real execution times then.

J=FCrgen

--=20
 The box said it requires Windows 95 or better so I installed Linux
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iQEVAwUBROucufy64gyiEfXtAQIXyAf+PGMyYBuPi+FIZ2sLYOgGNL8lfs5gJ/97
1lvmbhU17DVZ6+QfLCGslchyDNHLK4EgXsZB1LXVixMVDbaEGvic2IqV4eJ1D2B5
jnER7QkNhkaCGTx7T7pKNM2LCP7dFaVfzHl4dqYnlwi9ZZybYTPx9XynY0sH+pBM
I2xNzxLjz6KHJYBZyC2+qcVg4CbLwBuC8z+moYHkw1IXXYqAhPIKBdPSiPLh/kzK
Zyi25Y29V/CWgIqGMEeM4PrzBieXIRs0qg7/xzDrzCt28nV2SIJAySQdk/Asp9qD
6BBqMCNJDfHZ+fmeorUouf6eAxhf+W1H7e2U+a1oHh/ZrjRT5dD2OA==
=Gfk/
-----END PGP SIGNATURE-----