Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 22nd, 2007, 12:05 AM
mveygman@gmail.com
Guest
 
Posts: n/a
Default Performance of the std::map during a search

Hi,

I am writing code that is using std::map and having a bit of an issue
with its performance.

It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.

Has anyone run into this before?

  #2  
Old December 22nd, 2007, 12:05 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

mveygman@gmail.com wrote:
Quote:
Hi,
>
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>
The elements in a map are sorted on insertion, what are you attempting
to do?

--
Ian Collins.
  #3  
Old December 22nd, 2007, 01:15 AM
Ron AF Greve
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

Hi,


<mveygman@gmail.comwrote in message
news:1f4f63f2-3359-44a2-a8d4-ad6a35776931@c4g2000hsg.googlegroups.com...
Quote:
Hi,
>
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>
Has anyone run into this before?
>
Do you use map.find ? Otherwise post some code.

e.g.

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(){
map<string, stringDict;
Dict[ "boek" ] = "book";
Dict[ "winkel" ] = "shop";

map<string,string>::iterator Found = Dict.find( "boek" );
if( Found != Dict.end() )
{

cout << Found->first << " translates to " << Found->second << " (or
vice versa) " << endl;

}
else
{
cout << "No such word" << endl;
}
return 0;
}





Regards, Ron AF Greve

http://www.InformationSuperHighway.eu


  #4  
Old December 22nd, 2007, 01:55 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

On 2007-12-22 00:57, mveygman@gmail.com wrote:
Quote:
Hi,
>
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
For small collections that might be true, but for larger sets map should
be faster (if used correctly), unless perhaps if the vector is sorted
and you use a binary search.

--
Erik Wikström
  #5  
Old December 24th, 2007, 04:05 PM
mveygman@gmail.com
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

On Dec 22, 8:46 am, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2007-12-22 00:57, mveyg...@gmail.com wrote:
>
Quote:
Hi,
>
Quote:
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
Quote:
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>
For small collections that might be true, but for larger sets map should
be faster (if used correctly), unless perhaps if the vector is sorted
and you use a binary search.
>
--
Erik Wikström

Operating key word being "should".

The assumption that I went in with was that this was the case and
worst case scenario would be that serial search of the vector and
serial search of the map would be on par for time. They are not even
close.

Regards,

Mikhail
  #6  
Old December 24th, 2007, 04:15 PM
mveygman@gmail.com
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

On Dec 21, 8:07 pm, "Ron AF Greve" <ron@localhostwrote:
Quote:
Hi,
>
<mveyg...@gmail.comwrote in message
>
news:1f4f63f2-3359-44a2-a8d4-ad6a35776931@c4g2000hsg.googlegroups.com...
>
Quote:
Hi,
>
Quote:
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
Quote:
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>
Quote:
Has anyone run into this before?
>
Do you use map.find ? Otherwise post some code.
>
e.g.
>
#include <iostream>
#include <string>
#include <map>
using namespace std;
>
int main(){
map<string, stringDict;
Dict[ "boek" ] = "book";
Dict[ "winkel" ] = "shop";
>
map<string,string>::iterator Found = Dict.find( "boek" );
if( Found != Dict.end() )
{
>
cout << Found->first << " translates to " << Found->second << " (or
vice versa) " << endl;
>
}
else
{
cout << "No such word" << endl;
}
return 0;
>
}
>
Regards, Ron AF Greve
>
http://www.InformationSuperHighway.eu


Here is the test code:

#include <map>
#include <vector>
#include <iostream>
#include <ostream>
#include <sys/time.h /* gettimeofday */

unsigned long get_current_time()
{
struct timeval tv;
long usec;
gettimeofday (&tv, NULL);
usec = tv.tv_sec * 1000000;
usec += tv.tv_usec;
return usec;
}

int main(int argc, char **argv)
{
std::map< unsigned int, unsigned int test_map;
std::vector< unsigned int test_vector;

std::cout << "Setting up the test" << std::endl;

for (unsigned int i=0; i < 1000000; ++i)
{
test_map[i]=i;
test_vector.push_back(i);
}
std::random_shuffle(test_vector.begin(), test_vector.end());

time_t seed = time(NULL);

srand(seed);
unsigned long vec_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::vector< unsigned int >::const_iterator end_itr =
test_vector.end();
std::vector< unsigned int >::const_iterator itr =
test_vector.begin();
for( ; itr == end_itr ; ++itr )
{
if( *itr == value_to_find )
{
break;
}
}
}
unsigned long vec_end = get_current_time();

srand(seed);
unsigned long map_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::map< unsigned int, unsigned int >::iterator itr =
test_map.find(value_to_find);
}
unsigned long map_end = get_current_time();

std::cout << "Vec Test took " << (vec_end - vec_start) << "
microseconds. Map test took " << (map_end - map_start) << "
microseconds." << std::endl;
}
  #7  
Old December 24th, 2007, 04:45 PM
Hans Bos
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search


<mveygman@gmail.comwrote in message
news:d2e7c7db-4636-4e17-8c30-dd4aa5f41b9b@s19g2000prg.googlegroups.com...
Quote:
On Dec 21, 8:07 pm, "Ron AF Greve" <ron@localhostwrote:
Quote:
>Hi,
>>
><mveyg...@gmail.comwrote in message
>>
>news:1f4f63f2-3359-44a2-a8d4-ad6a35776931@c4g2000hsg.googlegroups.com...
>>
Quote:
Hi,
>>
Quote:
I am writing code that is using std::map and having a bit of an issue
with its performance.
>>
Quote:
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>>
Here is the test code:
>
....
Quote:
int main(int argc, char **argv)
{
std::map< unsigned int, unsigned int test_map;
std::vector< unsigned int test_vector;
>
std::cout << "Setting up the test" << std::endl;
>
for (unsigned int i=0; i < 1000000; ++i)
{
test_map[i]=i;
test_vector.push_back(i);
}
std::random_shuffle(test_vector.begin(), test_vector.end());
>
time_t seed = time(NULL);
>
srand(seed);
unsigned long vec_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::vector< unsigned int >::const_iterator end_itr =
test_vector.end();
std::vector< unsigned int >::const_iterator itr =
test_vector.begin();
for( ; itr == end_itr ; ++itr )
Since itr == end_itr is always false, the loop always exists immediatly, so you
don't search the vector at all.
This should be itr != end.
If I change this, the vector version takes a lot more time .
Quote:
{
if( *itr == value_to_find )
{
break;
}
}
}
unsigned long vec_end = get_current_time();
>
srand(seed);
unsigned long map_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::map< unsigned int, unsigned int >::iterator itr =
test_map.find(value_to_find);
}
unsigned long map_end = get_current_time();
>
std::cout << "Vec Test took " << (vec_end - vec_start) << "
microseconds. Map test took " << (map_end - map_start) << "
microseconds." << std::endl;
}
  #8  
Old December 24th, 2007, 05:55 PM
mveygman@gmail.com
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

On Dec 24, 11:44 am, "Hans Bos" <hans....@xelion.nlwrote:
Quote:
<mveyg...@gmail.comwrote in message
>
news:d2e7c7db-4636-4e17-8c30-dd4aa5f41b9b@s19g2000prg.googlegroups.com...
>
>
>
Quote:
On Dec 21, 8:07 pm, "Ron AF Greve" <ron@localhostwrote:
Quote:
Hi,
>
Quote:
Quote:
<mveyg...@gmail.comwrote in message
>
Quote:
Quote:
>news:1f4f63f2-3359-44a2-a8d4-ad6a35776931@c4g2000hsg.googlegroups.com...
>
Quote:
Quote:
Hi,
>
Quote:
Quote:
I am writing code that is using std::map and having a bit of an issue
with its performance.
>
Quote:
Quote:
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>
Quote:
Here is the test code:
>
...
Quote:
int main(int argc, char **argv)
{
std::map< unsigned int, unsigned int test_map;
std::vector< unsigned int test_vector;
>
Quote:
std::cout << "Setting up the test" << std::endl;
>
Quote:
for (unsigned int i=0; i < 1000000; ++i)
{
test_map[i]=i;
test_vector.push_back(i);
}
std::random_shuffle(test_vector.begin(), test_vector.end());
>
Quote:
time_t seed = time(NULL);
>
Quote:
srand(seed);
unsigned long vec_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::vector< unsigned int >::const_iterator end_itr =
test_vector.end();
std::vector< unsigned int >::const_iterator itr =
test_vector.begin();
for( ; itr == end_itr ; ++itr )
>
Since itr == end_itr is always false, the loop always exists immediatly, so you
don't search the vector at all.
This should be itr != end.
If I change this, the vector version takes a lot more time .
>
Quote:
{
if( *itr == value_to_find )
{
break;
}
}
}
unsigned long vec_end = get_current_time();
>
Quote:
srand(seed);
unsigned long map_start = get_current_time();
for(unsigned int i=0; i < 500000; ++i)
{
unsigned int value_to_find = rand() % 1000000;
std::map< unsigned int, unsigned int >::iterator itr =
test_map.find(value_to_find);
}
unsigned long map_end = get_current_time();
>
Quote:
std::cout << "Vec Test took " << (vec_end - vec_start) << "
microseconds. Map test took " << (map_end - map_start) << "
microseconds." << std::endl;
}

That would explain everyting. :)))
  #9  
Old December 25th, 2007, 11:55 AM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: Performance of the std::map during a search

On 2007-12-24 17:04, mveygman@gmail.com wrote:
Quote:
On Dec 22, 8:46 am, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
>On 2007-12-22 00:57, mveyg...@gmail.com wrote:
>>
Quote:
Hi,
>>
Quote:
I am writing code that is using std::map and having a bit of an issue
with its performance.
>>
Quote:
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
>>
>For small collections that might be true, but for larger sets map should
>be faster (if used correctly), unless perhaps if the vector is sorted
>and you use a binary search.
>>
>--
>Erik Wikström
>
>
Operating key word being "should".
>
The assumption that I went in with was that this was the case and
worst case scenario would be that serial search of the vector and
serial search of the map would be on par for time. They are not even
close.
Searching a map is O(log n) while a sequential search of a map is O(n),
which means that when the number of elements is large a map should be
faster. Exactly how many elements you need to have before the map is
faster depends on a lot of things but I would guess that with 1000
elements the map is almost always faster on a modern PC. On the other
hand if the vector is sorted then it might be faster searching the
vector using lower_bound() which is also O(log n), but most other
operations will still be faster on the map.

--
Erik Wikström
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles