remove the virtual keyword and it will compile ok with VC6.
Printable View
remove the virtual keyword and it will compile ok with VC6.
But in that case (i.e. remove virtual from the function) we cant get polymorphic behaviour.
It's just another one of the (very) many ways that VC++ 6 departs from the standard. Either you make sure you know the differences and live with them or get a new compiler. Some of us don't have the choice, so we have to live with it. It's annoying, but it's life.
Chambers:
Please do NOT cite speed and programming efficiency as an excuse for writting long functions. For one thing, I spend a good part of my programming life in boosting code performance. For two, some one on this board can care less about code perforamnce. More than once when I showed examples where a plain C solution beats the standard C++ template solution by more than 10 times in speed, they say who cares, ANSI C++ standard compilation is all that counts.
And for three, it becomes irrelevant to performance when you are talking about functions so long that when a variable is declared at the top of the function instead of near where it is used, it hurts readability. We are not talking about a three line function, where calling overhead makes a difference, we are talking about 500 lines of code versus 50 lines of code. The function call overhead no longer makes a difference.
When your function becomes so large it is hard to look for variables at the beginning of the function, variable declarations will NOT be the only thing that's unreadable. The whole function becomes unreadable, and you really need to cut the function down into smaller pieces.
It is my experience that when improving code speed performance, usually only 1% of the code, and possibly less, is relevant. Trying to optimize the other 99% could only do a dis-service.
I do view code efficiency, robustness, and performance as the ultimate goal every programmer should strive for. If my product runs 10 times as faster, and is 50% less in code size, and never throws any C++ exception, than a competitor's product, I could care less that mine is written in assembly and plain C, mixed with some VB, while my competitor can boast full ansi C++ compliance.
I will only comment on this, since large functions are usually to be avoided in all languages.Quote:
Originally posted by Anthony Mai
Chambers:
Please do NOT cite speed and programming efficiency as an excuse for writting long functions. For one thing, I spend a good part of my programming life in boosting code performance. For two, some one on this board can care less about code perforamnce. More than once when I showed examples where a plain C solution beats the standard C++ template solution by more than 10 times in speed, they say who cares, ANSI C++ standard compilation is all that counts.
I don't know what example you've shown to do this "speedup". It must be some super-contrived example that you must have come up with (you've done that before). It's hard to believe that unrolling a template causes a 10x speedup. Also C++ programmers have heard of something called a "profiler". They do use them, you know. Did you use a profiler on your code to see which parts were slow, or did you just (as always) knee-jerk blame C++ and went back to your 'C' coding? Second, (and I've said this many times), this is a C++ forum, where the solutions (whether you like them or not) will be C++ solutions. If someone posts to use try-catch, or use a template, or use a std::vector<>, is it your duty to say it's not a solution? If you went to the Java forum, would you post 'C' code, saying that Java is too slow?
And I know you don't want to hear this, but when C++ experts hear that "my program is slower in C++", the problem most of the time is that the programmer does not use the language or library properly, with the second (less of a reason) being that the compiler implementation is poor. It is not a problem with the C++ language in general. I can give you links that cite this, but why bother? Your "10x speed up" could be a "10x slowdown" on another compiler or on another platform.
Use whatever language you like. The problem is when you advocate other languages in a general C++ forum and with your advocacy, claim that the C++ solution is wrong.
Regards,
Paul McKenzie
I only follow up on what Paul only commented.
I do not blame Paul for bad memory, but check my post just a few days ago, June. 20th, at 4:50pm, in the thread about sorting array of structures. I pointed out that a simple C solution which uses qsort to sort the index to the structure array easily beat a standard C++ solution which uses the sort() template function. The speed-up is 10-20 times.
You can not have code that is both generic and is the best in performance. You can not have a pair of shoe that is "one-size fits all" and which also fits your feet perfectly. You can't use C++ template and achieve code performance.
And am I not talking about C++ in this board? As long as I write any class/structure that has member functions at all, it's C++. I am against SOME stuff in C++ standard, like templates. It is C++, not C, when you take away templates, exceptions etc., the stuff that is harmful to performance and robustness, away.
I challenge Paul or any one to write a piece of code that can sort 1 million structure which contains at least two strings, each of which is at least 1KB long, using std:sort() and other STL stuff, within ONE hour, on a regular PC. I will give you one week.
Meanwhile, in time, I will provide a solution which takes just a few minutes to do the same thing, using plain C and no assembly code will be used.
Here is the quote from your supposed speedup solution:Quote:
Originally posted by Anthony Mai
I only follow up on what Paul only commented.
I do not blame Paul for bad memory, but check my post just a few days ago, June. 20th, at 4:50pm, in the thread about sorting array of structures. I pointed out that a simple C solution which uses qsort to sort the index to the structure array easily beat a standard C++ solution which uses the sort() template function. The speed-up is 10-20 times.
You keep moving the goalposts in the middle of the game. So you sorted indices using qsort(). You did not qsort as would std::sort() would do, did you? You didn't do strcmp() did you? Comparing apples to oranges, don't you think? Don't you think that indices can also be sorted using std::sort()? Why not use your very same technique, but use std::sort() instead of qsort()? Trying to pull a fast-one?Quote:
A better thing to do is not to copy or move the objects at all, instead, create an integer array which contains indexes into the object array, and use qsort to sort just the index. After the indexes are sorted in proper order, access the objects through the indexes.
Also if I were you, I wouldn't mention that thread. It kind of exposes a few things about your knowledge of C++.
So basically, you are against C++. Templates are a big part of C++ programming, whether you accept them or not, as are exception handling and the standard library. When someone gives a correct answer pertaining to the C++ language, there is no reason to arrogantly chime in and say that the person is wrong (especially when they are not wrong).Quote:
And am I not talking about C++ in this board? As long as I write any class/structure that has member functions at all, it's C++. I am against SOME stuff in C++ standard, like templates.
This, of course, is your opinion.Quote:
It is C++, not C, when you take away templates, exceptions etc., the stuff that is harmful to performance and robustness, away.
Yeah, just sort the indices using std::sort(), just like you did with qsort(). People have quoted line and verse from the standard to you, they have given you links to the experts on the language that go contrary to whatever you have to say, they've suggested books, papers, etc. You've even claimed that the inventor of the language is wrong about his own language. Why waste any time showing you code?Quote:
I challenge Paul or any one to write a piece of code that can sort 1 million structure which contains at least two strings, each of which is at least 1KB long, using std:sort() and other STL stuff, within ONE hour, on a regular PC. I will give you one week.
Regards,
Paul McKenzie
I'm not seeing how the sorting of the indices would work. To sort the array of integers, which admittedly would be much faster than doing the structs themselves, you need to do some sort of analysis of the structs. How is it any more efficient to extrapolate the indices from the structs, then sorting, rather than just sorting the structs themselves? It's been a while since I've read much about sorting algorithms, so I have to plead ignorance on this one. :)
The saving would come in not having to shuffle potentially large objects around. They all stay where they are and you copy and move simple data types.
As Graham pointed out, your moving ints rather than entire strings. Basically, it looks something like this using std::sort:Quote:
Originally posted by Bob Davis
I'm not seeing how the sorting of the indices would work. To sort the array of integers, which admittedly would be much faster than doing the structs themselves, you need to do some sort of analysis of the structs. How is it any more efficient to extrapolate the indices from the structs, then sorting, rather than just sorting the structs themselves? It's been a while since I've read much about sorting algorithms, so I have to plead ignorance on this one. :)
So the only thing being done is the comparison, and not the moving of the data. Of course, you are not supposed to be able to do this using std::sort (according to one such soul here) ;)Code:#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
class SomeClass
{
public:
std::string fld1;
std::string fld2;
};
typedef std::vector<SomeClass> SomeClassVect;
typedef std::vector<int> IntVector;
struct SortFld
{
SortFld(const SomeClassVect& Vect, int which) : m_Vect(Vect), nw(which) { }
bool operator() (int first, int second)
{
if ( nw == 0)
return m_Vect[first].fld1 < m_Vect[second].fld1;
return m_Vect[first].fld2 < m_Vect[second].fld2;
}
private:
SomeClassVect m_Vect;
int nw;
};
using namespace std;
int main()
{
SomeClassVect SV;
SomeClass S;
IntVector IV(2);
IV[0] = 0;
IV[1] = 1;
S.fld1 = "ABC";
S.fld2 = "000";
SV.push_back(S);
S.fld1 = "000";
S.fld2 = "111";
SV.push_back(S);
cout << "Original fld1" << endl;
cout << SV[IV[0]].fld1 << " " << SV[IV[1]].fld1 << endl;
cout << "Sort by field 1" << endl;
sort(IV.begin(), IV.end(), SortFld(SV, 0));
cout << SV[IV[0]].fld1 << " " << SV[IV[1]].fld1 << endl;
cout << "Original fld2" << endl;
cout << SV[IV[0]].fld2 << " " << SV[IV[1]].fld2 << endl;
cout << "Sort by field 2" << endl;
sort(IV.begin(), IV.end(), SortFld(SV, 1));
cout << SV[IV[0]].fld2 << " " << SV[IV[1]].fld2 << endl;
}
Regards,
Paul McKenzie
That should read that you didn't copy the structures, as a "normal" (i.e. 2 param) version of std::sort would do.Quote:
Originally posted by Paul McKenzie
[B]You didn't do strcmp() did you? Comparing apples to oranges, don't you think?
Regards,
Paul McKenzie
Paul,
You are really embarrassing yourself by showing a piece of code that shows that you have no idea about how to write efficient code at all.
The code you show is far from meeting my challenge of sorting 1 million structures within one hour. Actually it will take probably one full day to do it.
I did a simple test with your code. Some modifications are made so it sort 8192 elements instead of two. And it takes 107 seconds to sort just 8192 elements, each contain just two string of just 3 charactors!!!! Full source code will be posted in my next message so any one can have a try.
Had you used std::sort directly on the structure array, you would have been better off. What's the point of sorting by index if you can not do it (do not know how to do it) faster?
What's your problem? Your problem is you create a whole copy of the original structure array, and sort on that copy. No wonder it is slow!!!
Now be serious. Are you up to the challenge at all to write a piece of code that can sort 1 million structures in less than one hour?
// This is Paul's original code, modified slightly so an array
//of 8192 structures are generated and then sorted.
// It takes nearly 2 minutes on my machine!!!
#include <windows.h>
#include <assert.h>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#define ELEMENT_COUNT 8192
class SomeClass
{
public:
std::string fld1;
std::string fld2;
};
typedef std::vector<SomeClass> SomeClassVect;
typedef std::vector<int> IntVector;
struct SortFld
{
SortFld(const SomeClassVect& Vect, int which) : m_Vect(Vect), nw(which) { }
bool operator () (int first, int second)
{
if ( nw == 0)
return m_Vect[first].fld1 < m_Vect[second].fld1;
return m_Vect[first].fld2 < m_Vect[second].fld2;
}
private:
SomeClassVect m_Vect;
int nw;
};
using namespace std;
string RandomAlphaString()
{
int i;
char tmp[4];
string result;
const char char_tbl1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i=0; i<3; i++)
{
tmp[i] = char_tbl1[((unsigned int)rand())%(sizeof(char_tbl1)-1)];
}
tmp[sizeof(tmp)-1] = 0x00;
result = tmp;
return result;
}
string RandomNumberString()
{
int i;
char tmp[4];
string result;
const char char_tbl2[] = "0123456789";
for (i=0; i<3; i++)
{
tmp[i] = char_tbl2[((unsigned int)rand())%(sizeof(char_tbl2)-1)];
}
tmp[sizeof(tmp)-1] = 0x00;
result = tmp;
return result;
}
int main()
{
int i, StartTime, EndTime;
IntVector IV(ELEMENT_COUNT);
SomeClassVect SV;
srand(GetTickCount());
for (i=0; i<ELEMENT_COUNT; i++)
{
SomeClass S;
IV[i] = i;
S.fld1 = RandomAlphaString();
S.fld2 = RandomNumberString();
SV.push_back(S);
}
cout << "Original fld1" << endl;
cout << SV[IV[0]].fld1 << " " << SV[IV[1]].fld1 << endl;
cout << "Sort by field 1" << endl;
StartTime = GetTickCount();
sort(IV.begin(), IV.end(), SortFld(SV, 0));
EndTime = GetTickCount();
cout << "Sorting " << ELEMENT_COUNT << " elements takes " << (EndTime-StartTime) << " ms" << endl;
cout << SV[IV[0]].fld1 << " " << SV[IV[1]].fld1 << endl;
cout << "Original fld2" << endl;
cout << SV[IV[0]].fld2 << " " << SV[IV[1]].fld2 << endl;
cout << "Sort by field 2" << endl;
sort(IV.begin(), IV.end(), SortFld(SV, 1));
cout << SV[IV[0]].fld2 << " " << SV[IV[1]].fld2 << endl;
}
Please, you know very little about C++. You should be ashamed just showing up here.Quote:
Originally posted by Anthony Mai
Paul,
You are really embarrassing yourself by showing a piece of code that shows that you have no idea about how to write efficient code at all.
Please go away.Quote:
The code you show is far from meeting my challenge of sorting 1 million structures within one hour. Actually it will take probably one full day to do it.
That code was to show that std::sort can sort indices i.e. demonstration purposes only. That code was not for any challenge of "1 million elements".With your track record, I highly doubt any one would touch your code.Quote:
I did a simple test with your code. Some modifications are made so it sort 8192 elements instead of two. And it takes 107 seconds to sort just 8192 elements, each contain just two string of just 3 charactors!!!! Full source code will be posted in my next message so any one can have a try.
See second paragraph.Quote:
Had you used std::sort directly on the structure array, you would have been better off. What's the point of sorting by index if you can not do it (do not know how to do it) faster?
I have no time for your sophistry. The thread was originally about whether a piece of code is ANSI compliant. Zeeshan mentioned line and verse from the standard. Then you chime in with a totally irrelevant comment about style, completely missing the point of the original question. Now you want to take the advantage of throwing this entire thread over to some ego trip that you are on. I am not going to take part in this, and if I weren't a good sport, I can provide links to some of the most bizarre things you've stated, outright insults, and totally irrelevant, misleading, and incorrect things about basic C++ and its usage, just to make the others here have a chuckle.Quote:
Now be serious. Are you up to the challenge at all to write a piece of code that can sort 1 million structures in less than one hour?
You posting 'C' code "in your next message" is your "Hail Mary pass" to save yourself (for those who are not familiar with American Football, that's the long, high pass that the team that is losing must throw completely down the field with the clock running out of time). I won't be providing any code to show anything. Now go along and pick another thread (and CodeGuru poster) to pick on. So far, you are really a person on an ego trip, and you've targeted individuals here (some aren't even posting anymore to CodeGuru) to go out of your way to prove what -- I have no idea.
Regards,
Paul McKenzie
Ok you guys -- quit your squabbling! I'm sick of you filling up my e-mail box with your blasted quarels. Send private e-mails to each other if you want to fight like children!