String Copy idiom

Mike Moroney moroney at jon.DEC
Tue Mar 12 09:00:58 AEST 1985


I think this is cute, how VAX/VMS beats Unix at its own game.  The VMS C
compiler generates code as good as or better than anything I have seen posted
so far!

	char *s,*t;
	while (*s++ = *t++);

generates (on a VAX 780):

		movb	(r2)+,(r1)+
		beql	sym.2
	sym.1:
		movb	(r2)+,(r1)+
		bneq	sym.1
	sym.2:

This is as fast as you can get.

	char *s,*t;
	while ((*s = *t) != '\0')
	{
	s++;
	t++;
	}

generates:

		movb	(r2),(r1)
		beql	sym.4
	sym.3:
		incl	r1
		incl	r2
		movb	(r2),(r1)
		bneq	sym.3
	sym.4:

The default settings of the C compiler were used (that is I didn't select any
"generate warp speed code" flags).  Notice I did NOT use "register char *"
since VAX C is at least as intelligent as you are when it decides what should
or should not go into registers.  In fact version 1 of the C compiler ignored
"register" definitions for that reason.  They put it back in V2.0 to appease
those who think they are smarter than the compiler (which treats it as a
"hint").  I have also seen a benchmark program where the identical C program
was compiled and run on identical VAX hardware, one running Unix, and one
running VMS.  The Unix program took 3 times as long to run as the VMS.  This
program (which did all integer arithmetic) used static variables, so it didn't
even have the benefit of automatically placing auto's in registers when
possible. I would think Unix, being 95% written in C, would at least have a
d at mn good C compiler.  Want to improve the throughput of your Unix system?
Recompile it in VAX/VMS C!

These are not the views of Digital, although I am sure Digital agrees with me.

						Mike Moroney
					..!decwrl!rhea!jon!moroney



More information about the Comp.lang.c mailing list