473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

simple assignment of structs vs. memcpy

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,

Mar 10 '07 #1
5 9755
aiooua wrote:
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.

Mar 10 '07 #2
"aiooua" <ai****@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #3
aiooua wrote:
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.

Mar 10 '07 #4
"aiooua" <ai****@gmail.comwrote in message
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".
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 10 '07 #5
On Mar 10, 4:33 pm, "santosh" <santosh....@gmail.comwrote:
aiooua wrote:
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.
thanks to all for the inputs. they were quite helpful.
thanks for your time.

Mar 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
2
by: ANaiveProgrammer | last post by:
Hi all I have been trying to send a struct over socket. I tried to assign the values of struct to unsigned char array. I have attached both .c files at the bottom. 'Client.c' takes a struct,...
7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
6
by: mark | last post by:
Hello, I have two variables: bool oldValues = {true, false}; bool newValues = {false, false}; Now I would like to copy to oldValues the values from newValues. I don't want to copy it one by...
5
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
3
by: foolish | last post by:
Hello. I have been developing a piece of software in which I am trying to use lists of structs, and structs containing these lists. What I am finding is that I am losing memory somewhere along the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.