Don't use Scanf()

t patterson tp at td2cad.intel.com
Fri Mar 18 07:41:32 AEST 1988


In article <2980 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <1140 at csun.UUCP> sef at csun.UUCP (Sean Fagan) writes:
>>printf("Hello world!\n") is slower than puts("Hello world!") which is slower
>>than write(1,"Hello world!\n",13).
>
>Using write() instead of the stdio routines is not necessarily a win, because
>non-UNIX systems may still have to fiddle with e.g. NL vs. CRLF conversions.
>Also note that (even on UNIX) the buffering which is usually done in stdio may
>let you write everything with a single system call; this can make stdio faster
>than multiple calls to write().

   for those of you arguing about the relative merits of printf,
puts, and write, here's some times for each of the three writing 
"hello world\n" to /dev/null 100,000 times on a uVAX II running Ultrix 2.0:

printf		18 sec		(wall-clock time, not user or sys time)
puts		15 sec
write		59 sec

following is a script of what I did (with white space added), then the
source of the three test programs. such a simple test doesn't say a 
whole lot. decide for yourself what it means.

Script started on Thu Mar 17 13:11:17 1988
ctdone> time tprintf >/dev/null
17.3u 0.5s 0:18 98% 7+21k 0+0io 0pf+0w

ctdone> time tputs >/dev/null
14.6u 0.4s 0:15 99% 4+21k 0+0io 0pf+0w

ctdone> time twrite >/dev/null
5.1u 53.4s 0:59 98% 1+3k 0+0io 0pf+0w

ctdone> time tprintf >/usr/tmp/t
17.4u 1.9s 0:19 98% 7+21k 5+154io 0pf+0w

ctdone> time tputs >/usr/tmp/t
14.8u 1.9s 0:17 97% 3+21k 6+155io 0pf+0w

ctdone> time twrite >/usr/tmp/t
4.7u 346.4s 6:02 96% 1+3k 7+192io 0pf+0w

ctdone> 
script done on Thu Mar 17 13:23:56 1988

tprintf.c:
	main() 
	{
		int i;
	
		for ( i = 100000; i > 0 ; i-- ) {
			printf("hello world\n");
		}
		exit(0);
	}

tputs.c:
	main()
	{
		int i;
	
		for ( i = 100000; i > 0 ; i-- ) {
			puts("hello world");
		}
		exit(0);
	}

twrite.c:
	main()
	{
		int i;
	
		for ( i = 100000; i > 0 ; i-- ) {
			write(1, "hello world\n", 12);
		}
		exit(0);
	}
--
  ..tp.. t patterson	domain:	tp%td2cad.intel.com at relay.cs.net
			path:   {ihnp4,cbosgd,uunet}!wucs1!tp
                  		{pyramid,hoptoad}!td2cad!tp
                  		{decwrl,hplabs,oliveb}!intelca!mipos3!td2cad!tp



More information about the Comp.lang.c mailing list