TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Edwards
from: Frank Adam
date: 1996-08-29 09:00:04
subject: Passing a var.

G'Day Paul,
 
-=> Quoting Paul Edwards to Frank Adam <=-

 PE> Show me the C++ code you think would be slower if written in C. Or
 PE> better  still, write both the C and C++ versions, and have a look 
 PE> at the assembler generated for each, and look at the 
 PE> relative code.  They should be the same (speedwise).  BFN.  
NO WAY, NO HOW am i getting into a C vs. C++ argument, but if i had to use 
structures and pass them to individual functions, i would assume that a 
single class using local(to it) variables would speed things up.
Having said that, the class may take longer to initialize,setup in memory
or whatever else.
Anyway, the following is a scenario where a class is better to use and
i would expect it to be a bit quicker.

#include 
#include 
#include 

struct Cstruct
{
 char *a,*b;
 };

void FooInit(struct Cstruct *s, char *p1, char *p2)  /* 3 vars passed */
{
 s->a = malloc(strlen(p1)+1);s->b = malloc(strlen(p2)+1);
 strcpy(s->a,p1);strcpy(s->b,p2);
 }

void FooDo(struct Cstruct *s)                /* 1 */
{
 printf("\n String : %s %s",s->a,s->b);
 }

void FooKill(struct Cstruct *s)              /* 1 */
{
 free(s->a);free(s->b);
 }

main()
{
 struct Cstruct x,y,z;
 FooInit(&x,"Hi","Frank"); 
 FooInit(&y,"Hi","Paul");
 FooInit(&z,"This is","C straight");
 FooDo(&x); FooDo(&y); FooDo(&z);
 return 0;                                              
 }


___----------------------------------------------------------------- 
 
#include 
#include 
#include 


class CPlus
{
 private :
     char *a,*b;
 public  :
     CPlus(char *p1, char *p2)      /* 2 passed */
     {
      a = new(char)(strlen(p1)+1);b = new(char)(strlen(p2)+1);
      strcpy(a,p1);strcpy(b,p2);
      }

     void FooDo()                     /* none */
     {
      printf("\n String : %s %s",a,b);
      }

     ~CPlus(){delete (a); delete (b);} /* none */
 };

int main()
{
 CPlus x("Hi","Frank");
 CPlus y("Hi","Paul");
 CPlus z("This is","C plusplus");
 x.FooDo(); y.FooDo() ; z.FooDo();
 return 0;
 }

Didn't have time to test it for speed, but the latter
""looks"" more 
efficient, the C++ version sets up a fair bit of extra data into the asm 
output. 


Hold the mail, just tested the runtimes..compiled on TC2, small.
                                                               
c2.exe Average elapsed time = 0.0253956 seconds
c1.exe Average elapsed time = 0.0245724 seconds
c1.exe is 0.0008232 seconds faster than c2.exe 

Dammit ! C1 is the C version. Don't you hate that ?!

Chucked it into tprof also, 'cause i'm a bit sus about that timer 
program. Especially now. :)
Ran 'em 10 times, came between .0082-.0088 for C2 and .0082-0.0087 for C1.
So although nothing in it the C did get the fastest time still.
                   
Standard disclaimer: 
Mind you the cpp exe is 300 bytes larger than the C so it probably took 
a little longer to load. 
I hope.....:) 
Isn't David (our C++ guru) going to help me out here ? :-)

  L8r Frank (fadam{at}ozemail.com.au).
                                    
___ Blue Wave/DOS v2.21
                                                         
 
         

--- Gash
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.