simple assignment of structs vs. memcpy

A

aiooua

hello,

please consider the following code:
---
typedef lots int; //lots of data.
typedef struct one_{
lots data;
}info;

int main(){
info i1,i2;
i1 = i2; //option 1
memcpy(i1,i2,sizeof(info)); //option2
}
---
from a performance point of view, is there anything that'd make me
chose one of the options over the other?
i'm assuming option 1 would perform better. is that assumption
correct?

thanks,
 
G

Guest

aiooua said:
hello,

please consider the following code:
---
typedef lots int; //lots of data.
typedef struct one_{
lots data;
}info;

int main(){
info i1,i2;
i1 = i2; //option 1
memcpy(i1,i2,sizeof(info)); //option2
}
---
from a performance point of view, is there anything that'd make me
chose one of the options over the other?
i'm assuming option 1 would perform better. is that assumption
correct?

Performance is outside of the scope of the C standard and differs
significantly between various systems. On the implementations I'm
aware of, there is no noticeable difference in performance, because
equivalent code is generated for both options.
 
K

Keith Thompson

aiooua said:
please consider the following code:
---
typedef lots int; //lots of data.
typedef struct one_{
lots data;
}info;

int main(){
info i1,i2;
i1 = i2; //option 1
memcpy(i1,i2,sizeof(info)); //option2
}

It's full of syntax errors. If you want to post a code sample, please
try compiling it first, then copy-and-paste it exactly.

Your meaning happens to be clear enough in this case, but in general
you'll save us a lot of time by posting real code.
from a performance point of view, is there anything that'd make me
chose one of the options over the other?
i'm assuming option 1 would perform better. is that assumption
correct?

There's no real reason to assume that either is faster. It's possible
that the assignment might be faster, because the compiler has more
specific information available to it, but if the structure is large
enough the difference is likely to be trivial. The C standard says
nothing about performance.

In most cases, it's best to write code to be as clear as possible, and
worry about optimization only if you find that it's a real problem.
 
S

santosh

aiooua said:
hello,

please consider the following code:
---
typedef lots int; //lots of data.
typedef struct one_{
lots data;
}info;

int main(){
info i1,i2;
i1 = i2; //option 1
memcpy(i1,i2,sizeof(info)); //option2
}
---
from a performance point of view, is there anything that'd make me
chose one of the options over the other?
i'm assuming option 1 would perform better. is that assumption
correct?

The C Standard specifies nothing about the efficiency of the output of
a piece of compiled C code. It's the purview of the compiler to
optimise the object code. This involves a lot of trade-offs and is
likely to be specific for a compiler and a machine, and a set of
compiler options.

I would not worry about such micro-optimisations. Clarity of code is
much more important. The program can, and should, be profiled for
speed bottlenecks and then corrective action can be taken from a
variety of angles. One such corrective action might involve using
memcpy for copying large structures, but it's something to be decided
on a case-by-case basis and no generalisations are possible.
 
M

Malcolm McLean

aiooua said:
hello,

please consider the following code:
---
typedef lots int; //lots of data.
typedef struct one_{
lots data;
}info;

int main(){
info i1,i2;
i1 = i2; //option 1
memcpy(i1,i2,sizeof(info)); //option2
}
---
from a performance point of view, is there anything that'd make me
chose one of the options over the other?
i'm assuming option 1 would perform better. is that assumption
correct?

thanks,
A naive compiler might call memcpy explicitly for the second case, but
generate inline code for the first. However most compilers are not naive and
will inline and optimise small memcpy()s.

Unless the compiler is pervesely written the furxt should never be less
efficient than the last. However personally I always prefer a call to
memcpy(), because it flags "here is a potentially long operation".
 
A

aiooua

The C Standard specifies nothing about the efficiency of the output of
a piece of compiled C code. It's the purview of the compiler to
optimise the object code. This involves a lot of trade-offs and is
likely to be specific for a compiler and a machine, and a set of
compiler options.

I would not worry about such micro-optimisations. Clarity of code is
much more important. The program can, and should, be profiled for
speed bottlenecks and then corrective action can be taken from a
variety of angles. One such corrective action might involve using
memcpy for copying large structures, but it's something to be decided
on a case-by-case basis and no generalisations are possible.

thanks to all for the inputs. they were quite helpful.
thanks for your time.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top