-
August 6th, 2008, 01:59 AM
#31
Re: Initializing an array data member?
Originally Posted by muse1987
And much more initializations.
So what good is an application that just initializes? Also, a trivial array has to be initialized also, whether it is at the point where it is declared or some other part of the program.
Depending on the programmer's skills. Nothing can be always good. If hand-written code is slower, that means the programmer isn't very skilled.
Read item 43 ("Prefer algorithm calls to hand-written loops") from Scott Meyer's book "Effective STL".
The reason why algorithms are used are maintainability, efficiency and correctness. Meyers lays out perfectly why algorithms are preferred over hand-written loops. I suggest you read his book before making claims of programmers not being very skilled, and the argument that compilers cannot optimize algorithms. I won't even discuss the maintainability and reusability factor.
"overwhelmingly complicated" means it's complicated for compiler, not for me.
I don't know whether you've taken compiler theory, but what usually cannot be optimized is code that overly uses pointers and pointer aliasing, which is almost always the case with a programmer trying to be smarter than the compiler. Using algorithms and functors is what makes a compiler able to optimize code (by inlining, and again discussed by Meyers). So using algroithms gives the code a much better chance of being optimized than hand written loops.
Then the maintainability factor comes into play (discussed briefly here, even though I stated previously I won't discuss it).
Other programmers may have to maintain all of that tricky coding that attempts to be smarter than the compiler's optimizer. Using standard algorithms allows other programmers to understand immediately what the code is supposed to be doing, leading to less bugs, less downtime, and more time to develop other parts of the application. If you're coding alone, then these factors may not matter to you, but it sure does matter when working on a project with multiple programmers, where the deliverable must be done in a certain time, have few if any bugs, and is maintainable and scalable.
As a matter of fact, everywhere I've worked, the project leaders would reject any code that is "home-made" if it's attempt is to mimic what an STL container or algorithm is supposed to accomplish.
And I forgot to mention one thing, the storage overhead. Suppose we need to int array of size 4,
Then just declare an array of 4 items.
It's absolutely abuse. So I'd recommend using built-in arrays if they can do the job.
And there is a point where an array of n items creates a larger than necessary footprint for an object,
Code:
class foo
{
int array[400];
// other members
};
//...
class foo
{
std::vector<int> array
public:
foo() : array(400) { }
};
Which foo is smaller in size? There is a point where using regular arrays are efficient. However those cases are small compared to the number of real world cases where the array must either be larger than 100 items, or that the array must be dynamic.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 6th, 2008 at 03:54 AM.
-
August 6th, 2008, 03:33 AM
#32
Re: Initializing an array data member?
Read item 43 ("Prefer algorithm calls to hand-written loops") from Scott Meyer's book "Effective STL".
The reason why algorithms are used are maintainability, efficiency and correctness. Meyers lays out perfectly why algorithms are preferred over hand-written loops. I suggest you read his book before making claims of programmers not being very skilled, and the argument that compilers cannot optimize algorithms. I won't even discuss the maintainability and reusability factor.
I fully agree with that.
I read the chapter and was convinced. My first thought nowadays when iterating through data is, 'can I use an algorithm and functor to do this?'. Updating old code has often reduced a dozen lines of code to a single algorithm call.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
August 6th, 2008, 05:49 AM
#33
Re: Initializing an array data member?
Originally Posted by Paul McKenzie
Also, a trivial array has to be initialized also, whether it is at the point where it is declared or some other part of the program.
Trivial arrays don't use the expensive heap memory.
Read item 43 ("Prefer algorithm calls to hand-written loops") from Scott Meyer's book "Effective STL".
The reason why algorithms are used are maintainability, efficiency and correctness. Meyers lays out perfectly why algorithms are preferred over hand-written loops. I suggest you read his book before making claims of programmers not being very skilled, and the argument that compilers cannot optimize algorithms. I won't even discuss the maintainability and reusability factor.
What I mean is hand-written codes can exploit extra informations for that specific case to optimize. I've experienced a lot of those cases. Most of time I ignore it just because it doesn't have much impact. However, if it's performance-critical portion, I write the loop myself and optimize it using those informations. I'm pretty sure that you would have seen people programming inner loops in assembly language.
Using algorithms and functors is what makes a compiler able to optimize code (by inlining, and again discussed by Meyers). So using algroithms gives the code a much better chance of being optimized than hand written loops.
In theroy, yes. But in practice, no. You can find it by disassembling this code.
Code:
int a[10] = {0};
std::vector<int> b( a, a+10 );
This is the code generated by MSVC2005 (/O2 with #define _SECURE_SCL 0). I'd happy to see C++ compiler which is significantly better than Visual C++.
Code:
00401046 mov eax,dword ptr [esp-3Ch]
0040104A sub esp,3Ch
0040104D push esi
0040104E push eax
0040104F lea ecx,[esp+0Ch]
00401053 push ecx
00401054 lea ecx,[esp+48h]
00401058 lea edx,[esp+20h]
0040105C call std::vector<int,std::allocator<int> >::_Construct<int *> (401150h)
00401150 push ebp
00401151 mov ebp,esp
00401153 push 0FFFFFFFFh
00401155 push offset __ehhandler$??$_Construct@PAH@?$vector@HV?$allocator@H@std@@@std@@QAEXPAH0Uinput_iterator_tag@1@@Z (402090h)
0040115A mov eax,dword ptr fs:[00000000h]
00401160 push eax
00401161 push ecx
00401162 push ebx
00401163 push esi
00401164 push edi
00401165 mov eax,dword ptr [___security_cookie (404018h)]
0040116A xor eax,ebp
0040116C push eax
0040116D lea eax,[ebp-0Ch]
00401170 mov dword ptr fs:[00000000h],eax
00401176 mov dword ptr [ebp-10h],esp
00401179 mov esi,dword ptr [ebp+8]
0040117C xor eax,eax
0040117E push eax
0040117F push esi
00401180 mov dword ptr [esi+4],eax
00401183 mov dword ptr [esi+8],eax
00401186 mov dword ptr [esi+0Ch],eax
00401189 mov dword ptr [ebp-4],eax
0040118C mov eax,dword ptr [ebp+8]
0040118F push eax
00401190 push ecx
00401191 push edx
00401192 call std::vector<int,std::allocator<int> >::_Insert<int *> (4012F0h)
00401197 mov ecx,dword ptr [ebp-0Ch]
0040119A mov dword ptr fs:[0],ecx
004011A1 pop ecx
004011A2 pop edi
004011A3 pop esi
004011A4 pop ebx
004011A5 mov esp,ebp
004011A7 pop ebp
004011A8 ret 8
004011AB mov esi,dword ptr [ebp+8]
004011AE call std::vector<int,std::allocator<int> >::_Tidy (4010B0h)
004011B3 xor eax,eax
004011B5 push eax
004011B6 push eax
004011B7 call _CxxThrowException (402026h)
--- and much more. Too much to extract all of them
Code:
class foo
{
int array[400];
// other members
};
//...
class foo
{
std::vector<int> array
public:
foo() : array(400) { }
};
Which foo() is smaller in size?
Absolutely the first one. Unless you change the constructor of second class, and if what you're considering is real memory usage rather than sizeof (foo).
There is a point where using regular arrays are efficient.
That's enough. Because I'm not talking about other cases. I think "... if they can do the job" is telling that point.
And in your example, if that class isn't allocated inside most-inner loop, definitely I'll use std::vector. However, when the performance becomes the first concern, I'd rather write
Code:
class foo
{
// other members
void* operator new( size_t, int Length );
void operator delete( void*, int );
void operator delete( void* );
int array[1];
};
I'd really be happy if C++ had template typedefs and void references, like void& / void const&.
When I program, I type, type, type... and eventually delete everything as my code contains buckets of bugs.
OMG so I'm programming to make bugs?
I'm happy when I meet someone who doesn't agree with me, because I can learn a lot by talking to him/her.
"Genius is one percent inspiration and ninety-nine percent perspiration." Thomas Edison
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|