Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For some reason I have to write my own sort function. I refer to std::sort, but sometime my function costs 2 times the time of std::sort.

So I copied "void _Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal, _Pr _Pred)" to my function sort2. What surprised me is that the time cost with sort2 is still 2 times of call std::sort directly. Why is the same code resulting in different speed?
Posted
Updated 22-Jun-10 21:48pm
v2

May be the implementation also specify some explicit specialization of that function for certain well known types (like int, long etc.) that have been compiled by the distributor with a different optimization flag and that you implicitly link from the vcxxx.dll (or lib, in case of static linking).

so, the source code is the same, but the translated binary is different.
 
Share this answer
 
When you copied the code did you just stick the code from std::sort into sort2 verbatim? i.e. sort2 isn't part of the std namespace. As it's not part of the std namespace it won't be using all the lovely specialisations for swap that are kicking around which may be the cause of the lack of speed.

Do a search for Koenig lookup (or argument dependent lookup - ADL) which explains how the compiler searches namespaces for names. I may be completely wrong but this sort of thing has bitten me in the past when I've moved stuff between namespaces.

Cheers,

Ash
 
Share this answer
 
Comments
lay_1980 23-Jun-10 20:49pm    
Reason for my vote of 1
Thanks a lot for your answer,but namespace is not the reason.
template<class _RanIt,
	class _Diff,
	class _Pr> inline
	void _Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal, _Pr _Pred)
	{	// order [_First, _Last), using _Pred
	_Diff _Count;
	for (;<big> _ISORT_MAX < (_Count = _Last - _First) </big>&& 0 < _Ideal; )
		{	// divide and conquer by quicksort
		pair<_RanIt, _RanIt> _Mid =
			_Unguarded_partition(_First, _Last, _Pred);
		_Ideal /= 2, _Ideal += _Ideal / 2;	// allow 1.5 log2(N) divisions
		if (_Mid.first - _First < _Last - _Mid.second)	// loop on larger half
			_Sort(_First, _Mid.first, _Ideal, _Pred), _First = _Mid.second;
		else
			_Sort(_Mid.second, _Last, _Ideal, _Pred), _Last = _Mid.first;
		}
	if (<big>_ISORT_MAX < _Count</big>)
		{	// heap sort if too many divisions
		std::make_heap(_First, _Last, _Pred);
		std::sort_heap(_First, _Last, _Pred);
		}
	else if (1 < _Count)
		_Insertion_sort(_First, _Last, _Pred);	// small, insertion sort
	}

This is the code of _Sort , heap sort is closed by _ISORT_MAX.
In release heap sort may be opened .

Changed the source ,my sort function is close to the speed of std::sort in 1M size vector.

const int _ISORT_MAX2 = 64*64;
for (;_ISORT_MAX2 < (_Count = _Last - _First) && 0 < _Ideal; )

Adjust the value of _ISORT_MAX2 , sort function will get different speed ,even quicker than std::sort.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900