A question of speed

J

Jerry

Can someone explain the underlying implications of this statement:
"#define P printf" ? I've been told that printf is called off of the
heap instead of the stacks, so P calls are much faster than just
"printf" calls. I thought it was just shorthand... Please CC via
e-mail when replying!

J
 
G

gabriel

Jerry said:
Can someone explain the underlying implications of this statement:
"#define P printf" ? I've been told that printf is called off of the
heap instead of the stacks, so P calls are much faster than just
"printf" calls. I thought it was just shorthand... Please CC via
e-mail when replying!

If you understand that this is a pre-processor statement, then you
understand that there is no performance difference at all.

Now, as to the advisability of coding like this...
 
P

pete

Jerry said:
Can someone explain the underlying implications of this statement:
"#define P printf" ? I've been told that printf is called off of the
heap instead of the stacks, so P calls are much faster than just
"printf" calls. I thought it was just shorthand...

What you have posted is shorthand, but not just shorthand,
it is superbad shorthand from The Source Code Of A Thousand Tears.

http://groups.google.com/[email protected]
Please CC via e-mail when replying!

OK, but I mung my return email.

All of the standard library functions may also be implemented as macros.
When there is a possibility that such a macro may evaluate
it's arguments more than once,
there is a special mention of it in the standard.

If the macro exists, then the statement
printf("\n");
will call either the macro or the function.

The statement
(printf)("\n");
will call the function.
 
B

Ben Pfaff

Can someone explain the underlying implications of this statement:
"#define P printf" ? I've been told that printf is called off of the
heap instead of the stacks, so P calls are much faster than just
"printf" calls. I thought it was just shorthand... Please CC via
e-mail when replying!

Are you sure you're not trolling?
 
D

Dan Pop

In said:
Can someone explain the underlying implications of this statement:
"#define P printf" ? I've been told that printf is called off of the
heap instead of the stacks, so P calls are much faster than just
"printf" calls. I thought it was just shorthand...

If you understand how the preprocessor works, it is enough to engage your
brain to get the answer. Otherwise, you'd better learn how the
preprocessor works, ASAP.

Dan
 
J

Jerry

My C experience is horribly rusty (haven't written anything solid in
about 6 or 7 years). The code I posted was from my boss's program and
I was told that by using that statement, his application ran "P"
commands faster than "printf" would have run because the compiler is
reading from the heap or something to that effect. That sounded a bit
suspect and since I have no point of reference, I was hoping you guys
could either validate that theory or run it into the ground. ;)

-J
 
K

Kevin D. Quitt

The effect of the macro is to have the pre-processor replace "P" with
"printf". There is therefore no speed difference. Perhaps your boss is
pulling your leg, or perhaps he's using a magic compiler. Bet him a
nickel they run the same speed.
 
P

Peter Nilsson

pete said:
What you have posted is shorthand, but not just shorthand,
it is superbad shorthand from The Source Code Of A Thousand Tears.

http://groups.google.com/[email protected]


OK, but I mung my return email.

All of the standard library functions may also be implemented as macros.

More specifically, they may be implemented as function-like macros.

In other words...

#define printf __printf

....is not a possibility within <stdio.h>.
 
R

Randy Howard

moriority5 said:
My C experience is horribly rusty (haven't written anything solid in
about 6 or 7 years). The code I posted was from my boss's program and
I was told that by using that statement, his application ran "P"
commands faster than "printf" would have run because the compiler is
reading from the heap or something to that effect.

Now you know why bosses shouldn't program. :)

That's not even remotely true.
 
S

shinydonkey

Randy said:
@yahoo.com says...

Now you know why bosses shouldn't program. :)

That's not even remotely true.

Actually, I just rediscovered this post, did a quick Google search, and
found where this code came from. Rather than from "The Source Code of
a Thousand Tears," it's the source to Arthur Whitney's J Interpreter.
So not only was it not explained correctly, it seems to have been
plagiarized. :)

The original "amazing" code:

typedef char C;typedef long I;
typedef struct a{I t,r,d[3],p[2];}*A;
#define P printf
#define R return
#define V1(f) A f(w)A w;
#define V2(f) A f(a,w)A a,w;
#define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
I *ma(n){R(I*)malloc(n*4);}mv(d,s,n)I *d,*s;{DO(n,d=s);}
tr(r,d)I *d;{I z=1;DO(r,z=z*d);R z;}
A ga(t,r,d)I *d;{A z=(A)ma(5+tr(r,d));z->t=t,z->r=r,mv(z->d,d,r);R z;}
V1(iota){I n=*w->p;A z=ga(0,1,&n);DO(n,z->p=i);R z;}
V2(plus){I r=w->r,*d=w->d,n=tr(r,d);A z=ga(0,r,d);
DO(n,z->p=a->p+w->p);R z;}
V2(from){I r=w->r-1,*d=w->d+1,n=tr(r,d);
A z=ga(w->t,r,d);mv(z->p,w->p+(n**a->p),n);R z;}
V1(box){A z=ga(1,0,0);*z->p=(I)w;R z;}
V2(cat){I an=tr(a->r,a->d),wn=tr(w->r,w->d),n=an+wn;
A z=ga(w->t,1,&n);mv(z->p,a->p,an);mv(z->p+an,w->p,wn);R z;}
V2(find){}
V2(rsh){I r=a->r?*a->d:1,n=tr(r,a->p),wn=tr(w->r,w->d);
A z=ga(w->t,r,a->p);mv(z->p,w->p,wn=n>wn?wn:n);
if(n-=wn)mv(z->p+wn,z->p,n);R z;}
V1(sha){A z=ga(0,1,&w->r);mv(z->p,w->d,w->r);R z;}
V1(id){R w;}V1(size){A z=ga(0,0,0);*z->p=w->r?*w->d:1;R z;}
pi(i){P("%d ",i);}nl(){P("\n");}
pr(w)A w;{I r=w->r,*d=w->d,n=tr(r,d);DO(r,pi(d));nl();
if(w->t)DO(n,P("< ");pr(w->p))else DO(n,pi(w->p));nl();}

C vt[]="+{~<#,";
A(*vd[])()={0,plus,from,find,0,rsh,cat},
(*vm[])()={0,id,size,iota,box,sha,0};
I st[26]; qp(a){R a>='a'&&a<='z';}qv(a){R a<'a';}
A ex(e)I *e;{I a=*e;
if(qp(a)){if(e[1]=='=')R st[a-'a']=ex(e+2);a= st[ a-'a'];}
R qv(a)?(*vm[a])(ex(e+1)):e[1]?(*vd[e[1]])(a,ex(e+2)):(A)a;}
noun(c){A z;if(c<'0'||c>'9')R 0;z=ga(0,0,0);*z->p=c-'0';R z;}
verb(c){I i=0;for(;vt;)if(vt[i++]==c)R i;R 0;}
I *wd(s)C *s;{I a,n=strlen(s),*e=ma(n+1);C c;
DO(n,e=(a=noun(c=s))?a:(a=verb(c))?a:c);e[n]=0;R e;}

main(){C s[99];while(gets(s))pr(ex(wd(s)));}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top