 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thomas Richter Guest
|
Posted: Mon Apr 23, 2012 1:31 pm Post subject: Re: which one is faster? |
|
|
On 05.04.2012 01:05, echo ma wrote:
| Quote: | on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
|
Sorry, I don't understand. To have something "the same", you need to
compare two different things. So which two "are the same" or "not the
same"? I see only one piece of code, and this is not even portable.
(#pragma pack() is not ANSI-C).
Anyhow, before performing micro-optimizations like the one you made
above, profile your code and identify whether this is really the cause
of a performance (or memory) problem. You should probably post a more
complete example to allow any judgement - and measure first before
trying non-portable constructs.
Greetings,
Thomas
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Dag-Erling Smørgrav Guest
|
Posted: Mon Apr 23, 2012 1:31 pm Post subject: Re: portable code |
|
|
rangsynth (AT) gmail (DOT) com writes:
| Quote: | You can say
write((i >> 24) & 0xFF)
write((i >> 16) & 0xFF)
write((i >> & 0xFF)
write(i & 0xFF)
|
There is no write() in C, but there is one in POSIX that doesn't match
this usage:
ssize_t write(int fd, void *buf, size_t len);
Let's assume for now that this in fact a private function based on
a global FILE *f:
void write(unsigned char i)
{
if (fwrite(&i, 1, 1, f) != 1) {
fprintf(stderr, "fwrite() failed\n");
exit(EX_FAILURE);
}
}
(actually, we should assume that this function is called funwrite() and
that write() is the following macro:
#define write(i) funwrite(i);
but I'm being charitable)
| Quote: | int out = 0;
out += (read() >> 24);
out += (read() >> 16);
out += (read() >> ;
out += (read());
|
There is no read() in C, but there is one in POSIX that doesn't match
this usage:
ssize_t read(int fd, void *buf, size_t len);
Let's assume for now that this is in fact a private function based on a
global FILE *f:
unsigned char read(void)
{
unsigned char i;
if (fread(&i, 1, 1, f) != 1) {
fprintf(stderr, "fread() failed\n");
exit(EX_FAILURE);
}
return i;
}
then the following:
unsigned int i = 0x01020304;
write((i >> 24) & 0xFF);
write((i >> 16) & 0xFF);
write((i >> & 0xFF);
write(i & 0xFF);
rewind(f);
i = 0;
i += (read() >> 24);
i += (read() >> 16);
i += (read() >> ;
i += (read());
printf("i = 0x%x\n", i);
will print
i = 0x4
The correct answer is "store your data in a textual representation".
DES
--
Dag-Erling Smørgrav - des (AT) des (DOT) no
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Mon Apr 23, 2012 1:32 pm Post subject: Re: which one is faster? |
|
|
On Wed, 4 Apr 2012 18:05:42 -0500 (CDT), echo ma <fatmck (AT) gmail (DOT) com>
wrote:
| Quote: | on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
Sorry for my poor english.
|
It will depend on your system (hardware, compiler, and options).
There is no guarantee that
t.a = 0;
and
t.a = 1;
will have the same performance. (Some systems have methods of zeroing
memory that are more efficient than storing an arbitrary value.)
For that matter, there is no guarantee that
t.a = 0;
and
t.b = 0;
have the same performance. (On some systems, access performance is
different for different alignments.)
And then step three accesses "more" memory than the first two so why
should its performance be the same.
The only way to answer these types of questions is to benchmark your
code and even that is questionable unless you disable cache and your
code is the ONLY process executing on the system, something pretty
rare on most user systems these days.
--
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Mon Apr 23, 2012 1:32 pm Post subject: Re: which one is faster? |
|
|
echo ma <fatmck (AT) gmail (DOT) com> writes:
| Quote: | on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
|
The C language standard says nothing about the relative performance
of various operations, so there is no general answer to your
question. The language specifies the behavior of programs, not
their performance.
You can answer it for your own system by measuring the performance.
Suppose you find out that one operation is faster than another on
your system; what would you do with that information? If you need
to assign a value to t.c, assigning a value to t.a might be faster,
but that doesn't do yhou much good if it's not the right thing to do.
(Note that "#pragma pack" is non-standard and potentially unsafe;
see <http://stackoverflow.com/q/8568432/827263>. For your particular
struct definition, it's unlikely to have any effect.)
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bharat Guest
|
Posted: Mon Apr 23, 2012 1:32 pm Post subject: Re: portable code |
|
|
See
http://code.google.com/p/protobuf-c/
http://en.wikipedia.org/wiki/Type-length-value
you will have portable at the cost of slight cpu overhead.
On Friday, 17 December 2010 00:15:38 UTC+5:30, raffamaiden wrote:
| Quote: | Hi all. I'm writing a program wich will write some variables to an
output file. I do something like
int a =5;
fwrite(&a, sizeof(int), 1, my_file_ptr);
This will write an int to the file pointed by my_file_ptr. But i know
that the c standard does not specify the exact size in bytes for its
primitive type, as far as i know it only specifies that and int is an
integer type that rapresents a number with a sign, but different
implementations\operating systems can have different size for an int.
So this mean that my program will write a 32 bit integer with one
implementation and a 16 bit with another implementation. This would
also mean that the file generated by the program that is running in
one implementation will not be readable in another implementation,
unless the program knows also in which implementation the instance
that generated the file was running.
That is right? I do not want such a behavior. How can i solve this?
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-- |
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
echo ma Guest
|
Posted: Mon Apr 23, 2012 1:32 pm Post subject: Re: portable code |
|
|
You can use int32_t in <stdint.h>, this is part of C99. You should use int32_t in both source code and the file format document, so other developers will know the size of your int.
Another thing you should know, the byte order things. Intel X86 and IA64 are using little endian, while IBM PowerPC and Sparc are using big endian. Memory layout are totally defferent when you assign 5 for an interger variable on there CPUs. You should use the a uniform byte order independent of CPUs, so you would like to use htons() and htonl() to made it. Or, you can use text representations, this is more portable and easy for human reading.
Sorry for my poor English. Wish this help you.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
echo ma Guest
|
Posted: Mon Apr 23, 2012 1:33 pm Post subject: Re: C Data Structures Libraries |
|
|
在 2011年2月5日星期六UTC+8上午2时46分35秒,Nimo写道:
| Quote: | Hello,
where can I find free C Data Structures Libraries..
Thank You
cheers
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
You must mean the common List / Map / Queue data structures.
I want them too, currently always using STL in C++...
I know there are double-linked-list and fifo implemented by linux kernel in C, but I have never used them. Check on this page: http://www.kernel.org/doc/htmldocs/kernel-api/
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Joel C. Salomon Guest
|
Posted: Mon Apr 23, 2012 1:33 pm Post subject: Re: which one is faster? |
|
|
More CPU cycles have been spent passing this message across the Internet
than would be saved by any such optimization.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
David Brown Guest
|
Posted: Mon Apr 23, 2012 1:33 pm Post subject: Re: which one is faster? |
|
|
On 05/04/2012 01:05, echo ma wrote:
| Quote: | on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
|
Taken out of context, it is impossible to say. The speed of code
depends on a huge number of factors, including the compiler, compiler
options, processor (there are several dozens of commonly used 32-bit
processor architectures), cache, memory, etc., as well as the
surrounding code.
As a wild generalisation, the timing will be much the same if the
processor uses absolute addressing modes, but for processors that make
more use of pointers there will be some initial code to set up the
pointer before the first access, then each access will be quickly done.
| Quote: | Sorry for my poor english.
|
There is no problem with your English - the problem lies in not knowing
what you are trying to ask. It is seldom a good plan to take statements
out of context and ask if that one statement is fast or slow - you have
to look at the bigger picture.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Tue May 01, 2012 2:57 am Post subject: Re: which one is faster? |
|
|
Thomas Richter <thor (AT) math (DOT) tu-berlin.de> writes:
| Quote: | On 05.04.2012 01:05, echo ma wrote:
on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
Sorry, I don't understand. To have something "the same", you need to
compare two different things. So which two "are the same" or "not the
same"? I see only one piece of code, and this is not even portable.
(#pragma pack() is not ANSI-C).
|
I think echo ma is asking about the relative performance of the three
statements "t.a = 0;", "t.b = 1;", and "t.c = 2;".
I suppose if, for example, the assignment to t.a turned out to be
significantly faster than the assignment to t.b, then it might make
sense to rearrange the struct definition so the most frequently accessed
members are first.
In practice, such rearrangement is unlikely to be helpful.
[...]
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
George Neuner Guest
|
Posted: Tue May 01, 2012 2:58 am Post subject: Re: which one is faster? |
|
|
On Mon, 23 Apr 2012 08:32:16 -0500 (CDT), Keith Thompson
<kst-u (AT) mib (DOT) org> wrote:
| Quote: | echo ma <fatmck (AT) gmail (DOT) com> writes:
on a 32bit system.
#pragma pack(4)
struct TestStruct
{
unsigned short a;
unsigned short b;
unsigned int c;
};
#pragma pack()
struct TestStruct t;
t.a = 0; //step 1
t.b = 1; //step 2
t.c = 2; //step 3
Questing is : Are these 3 steps having the same perfomance time?
Suppose you find out that one operation is faster than another on
your system; what would you do with that information? If you need
to assign a value to t.c, assigning a value to t.a might be faster,
but that doesn't do yhou much good if it's not the right thing to do.
(Note that "#pragma pack" is non-standard and potentially unsafe;
see <http://stackoverflow.com/q/8568432/827263>. For your particular
struct definition, it's unlikely to have any effect.)
|
The only real reasons for such tightly packed structures are to save
memory in *very* small systems, or to perform memory mapped device
access ... which brings up both coding issues such as register value
reuse and CPU/platform issues such as bus width, access alignment,
write-back caching, out-of-order execution, write combining, etc.
x86 being an exception, most 32-bit CPUs use memory mapped I/O
exclusively, and it often is desirable to create structures for
accessing groups of contiguous device "registers". However, in such
cases, making a single 32-bit access to A+B in order to write a 16-bit
value to B would be incorrect (and potentially destructive).
As someone else said already, it would be nice if the OP had given a
more complete description of the intended use.
George
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jorgen Grahn Guest
|
Posted: Tue May 01, 2012 2:59 am Post subject: Re: portable code |
|
|
On Mon, 2012-04-23, Dag-Erling Smrgrav wrote:
| Quote: | rangsynth (AT) gmail (DOT) com writes:
You can say
write((i >> 24) & 0xFF)
write((i >> 16) & 0xFF)
write((i >> & 0xFF)
write(i & 0xFF)
There is no write() in C, but there is one in POSIX that doesn't match
this usage:
|
I am pretty sure rangsynth just used "write" as a generic "write an
octet to somewhere" function. The details are irrelevant here.
[snip irrelevant]
| Quote: | but I'm being charitable)
int out = 0;
out += (read() >> 24);
out += (read() >> 16);
out += (read() >> ;
out += (read());
There is no read() in C, but there is one in POSIX that doesn't match
this usage:
|
[snip irrelevant]
| Quote: | then the following:
unsigned int i = 0x01020304;
write((i >> 24) & 0xFF);
write((i >> 16) & 0xFF);
write((i >> & 0xFF);
write(i & 0xFF);
rewind(f);
i = 0;
i += (read() >> 24);
i += (read() >> 16);
i += (read() >> ;
i += (read());
printf("i = 0x%x\n", i);
will print
i = 0x4
|
If it does, it's because you introduced a bug somewhere. Why? I feel
I'm missing some point here; can you please explain it more clearly?
| Quote: | The correct answer is "store your data in a textual representation".
|
I prefer that too, but you don't always get to choose your requirements.
Especially not file formats and protocols!
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bharat Guest
|
Posted: Tue May 01, 2012 2:59 am Post subject: Re: C Data Structures Libraries |
|
|
did you try google?
here is the first listing i got
http://home.gna.org/gdsl/
On Saturday, 5 February 2011 00:16:35 UTC+5:30, Nimo wrote:
| Quote: | Hello,
where can I find free C Data Structures Libraries..
Thank You
cheers
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-- |
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bharat Guest
|
Posted: Tue May 01, 2012 3:00 am Post subject: Re: portable code |
|
|
On Friday, 17 December 2010 00:15:38 UTC+5:30, raffamaiden wrote:
| Quote: | Hi all. I'm writing a program wich will write some variables to an
output file. I do something like
int a =5;
fwrite(&a, sizeof(int), 1, my_file_ptr);
This will write an int to the file pointed by my_file_ptr. But i know
that the c standard does not specify the exact size in bytes for its
primitive type, as far as i know it only specifies that and int is an
integer type that rapresents a number with a sign, but different
implementations\operating systems can have different size for an int.
So this mean that my program will write a 32 bit integer with one
implementation and a 16 bit with another implementation. This would
also mean that the file generated by the program that is running in
one implementation will not be readable in another implementation,
unless the program knows also in which implementation the instance
that generated the file was running.
That is right? I do not want such a behavior. How can i solve this?
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
See usage of TLV
http://en.wikipedia.org/wiki/Type-length-value
See http://code.google.com/p/protobuf/
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Dag-Erling Smørgrav Guest
|
Posted: Fri May 04, 2012 10:29 pm Post subject: Re: portable code |
|
|
Jorgen Grahn <grahn+nntp (AT) snipabacken (DOT) se> writes:
| Quote: | Dag-Erling Smørgrav <des (AT) des (DOT) no> writes:
There is no write() in C, but there is one in POSIX that doesn't match
this usage:
I am pretty sure rangsynth just used "write" as a generic "write an
octet to somewhere" function. The details are irrelevant here.
|
The details are not irrelevant. When you post code that operates on the
value returned by a function, the semantics of that function and the
range and representation of its return value are highly relevant.
| Quote: | then the following:
unsigned int i = 0x01020304;
write((i >> 24) & 0xFF);
write((i >> 16) & 0xFF);
write((i >> & 0xFF);
write(i & 0xFF);
rewind(f);
i = 0;
i += (read() >> 24);
i += (read() >> 16);
i += (read() >> ;
i += (read());
printf("i = 0x%x\n", i);
will print
i = 0x4
If it does, it's because you introduced a bug somewhere. Why? I feel
I'm missing some point here; can you please explain it more clearly?
|
I didn't introduce a bug anywhere. On the contrary - I was pointing out
a bug in rangsynth's code. I'm sure you'll see it too if you look
closely.
DES
--
Dag-Erling Smørgrav - des (AT) des (DOT) no
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|