Thomas Hruska wrote:
> Nico Heinze wrote:
>> --- In [email protected], "Indika Bandara" <[EMAIL PROTECTED]>
>> wrote:
>>> man qsort
>>> ==================================================
>>>  
>>>        void qsort(void *base, size_t nmemb, size_t size,
>>>                   int(*compar)(const void *, const void *));
>>>  
>>> DESCRIPTION
>>> The qsort() function sorts an array with nmemb elements of size
>>> size.  The base argument points to the start of the array.
>>>  
>>> The  contents of the array are sorted in ascending order
>>> according to a comparison function pointed to by compar, which is
>>> called with two arguments
>>> that point to the objects being compared.
>>> ==================================================
>>>
>>> as man says, "sorts an array with nmemb elements of size size."
>>> my array contains MAX_NUM of (P*) 
>>>>>         P** p = new P*[MAX_NUM];
>>> but if it were 
>>> int *i = new int[MAX_NUM]
>>> and 
>>> qsort(i, MAX_NUM, sizeof(i), CompInt);
>>> works
>>>
>>> am i doing something silly? can u pls try urself?
>> <snip>
>>
>> I won't; I don't have the time. But even if I did have the time:
>> frankly I don't understand C++ well enough to even judge whether
>> qsort() can be used with objects this way. I have to leave the
>> response to this question to other people like Victor, Paul, and many
>> other ones. Sorry. I'm out of the game here.
>>
>> Regards,
>> Nico
> 
> qsort() is C...
> (sort() is C++ - the OP is mixing C and C++ though)
> 
> Anyway to answer the OP's question:
> 
>          qsort(p, MAX_NUM, sizeof(P*), Comp);
> 
> Should be:
> 
>          qsort(p, MAX_NUM, sizeof(P**), Comp);
> 
> (Although it probably won't affect anything).
> Then this:
> 
> int Comp(const void* p1, const void* p2)
> {
>          P* i1 = (P*)p1;
>          P* i2 = (P*)p2;
>          if(i1->_i>i2->_i)
>                  return 1;
>          if(i1->_i<i2->_i)
>                  return -1;
> 
>          return 0;
> }
> 
> Should be:
> 
> int Comp(const void *p1, const void *p2)
> {
>          P *i1 = *((P **)p1);
>          P *i2 = *((P **)p2);
>          if(i1->_i>i2->_i)
>                  return 1;
>          if(i1->_i<i2->_i)
>                  return -1;
> 
>          return 0;
> }
> 
> However, since you are using C++, you should be using std::sort(), 
> FastSort() (from Safe C++ Design Principles - free e-book for group 
> members), or equivalent with an appropriate sort class in P that 
> overloads the () operator.

Oops - forgot to mention why.  See, qsort() can't inline the comparison 
function because, well, it is C.  It is a function.  This means that you 
incur the overhead of a function call for each and every comparison.  On 
the other hand, C++ can use templates, which means it CAN inline the 
comparison function.  This means that there is no function call 
overhead.  As a result, you see anywhere from a 25 to 50% increase in 
performance.  Sorting is one area where C++ blows C out of the water in 
terms of performance (although equivalent performance can be achieved by 
rolling custom sorting functions...why bother?  That's a huge waste of 
time).

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to