This is weird, could it be stl bug in std::distance?
I compare stl and Qt container, when totalItems is more than a certain number, say 10000, the stl becomes extremely slow but QList is fine...
#include <QList>
#include <set>
#include <iostream>
#include <math.h>
static int findIndex( const std::set<int>& stlSet, int value )
{
std::set<int>::const_iterator it = stlSet.find( value );
if ( it != stlSet.end() ) {
return std::distance( stlSet.begin(), it );
}
return -1;
}
static void testStl( int totalItems )
{
std::set<int> stlSet;
for ( int idx = 0; idx < totalItems; idx++ ) {
stlSet.insert( idx );
}
for ( int idx = 0; idx < totalItems; idx++ ) {
//std::cout << findIndex( stlSet, idx ) << std::endl;
findIndex( stlSet, idx );
}
}
static int findIndex( const QList<int>& qlists, int value )
{
QList<int>::const_iterator it = qBinaryFind( qlists, value );
if ( it != qlists.end() ) {
return it - qlists.begin();
}
return -1;
}
static void testQList( int totalItems )
{
QList<int> qlists;
for ( int idx = 0; idx < totalItems; idx++ ) {
qlists << idx;
}
for ( int idx = 0; idx < totalItems; idx++ ) {
//std::cout << findIndex( qlists, idx ) << std::endl;
findIndex( qlists, idx );
}
}
int main( int argc, char** argv )
{
int totalItems = atoi( argv[1] );
std::cout << "Start testQList" << std::endl;
testQList( totalItems );
std::cout << "End testQList\n" << std::endl;
std::cout << "Start testStl" << std::endl;
testStl( totalItems );
std::cout << "End testStl" << std::endl;
}
#include <QList>
#include <set>
#include <iostream>
#include <math.h>
static int findIndex( const std::set<int>& stlSet, int value )
{
std::set<int>::const_iterator it = stlSet.find( value );
if ( it != stlSet.end() ) {
return std::distance( stlSet.begin(), it );
}
return -1;
}
static void testStl( int totalItems )
{
std::set<int> stlSet;
for ( int idx = 0; idx < totalItems; idx++ ) {
stlSet.insert( idx );
}
for ( int idx = 0; idx < totalItems; idx++ ) {
//std::cout << findIndex( stlSet, idx ) << std::endl;
findIndex( stlSet, idx );
}
}
static int findIndex( const QList<int>& qlists, int value )
{
QList<int>::const_iterator it = qBinaryFind( qlists, value );
if ( it != qlists.end() ) {
return it - qlists.begin();
}
return -1;
}
static void testQList( int totalItems )
{
QList<int> qlists;
for ( int idx = 0; idx < totalItems; idx++ ) {
qlists << idx;
}
for ( int idx = 0; idx < totalItems; idx++ ) {
//std::cout << findIndex( qlists, idx ) << std::endl;
findIndex( qlists, idx );
}
}
int main( int argc, char** argv )
{
int totalItems = atoi( argv[1] );
std::cout << "Start testQList" << std::endl;
testQList( totalItems );
std::cout << "End testQList\n" << std::endl;
std::cout << "Start testStl" << std::endl;
testStl( totalItems );
std::cout << "End testStl" << std::endl;
}
To copy to clipboard, switch view to plain text mode
Bookmarks