Comments on: STL is Slow Template Library http://floatingsun.net/2006/10/19/stl-is-slow-template-library/?utm_source=rss&utm_medium=rss&utm_campaign=stl-is-slow-template-library Sat, 11 May 2013 19:51:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Diwaker Gupta http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-198847 Diwaker Gupta Mon, 12 Apr 2010 17:42:38 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-198847 @theoracle: I think it will be a lot more valuable if your comment explained why the post is irrelevant and misleading, rather than simply deleting the post. That way, others who come across this (search engines never forget) can get the context and walk away with new insights.

]]>
By: theoracle http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-198806 theoracle Sun, 11 Apr 2010 08:46:50 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-198806 Please delete this post. It is irrelevant and misleading.

]]>
By: Gareth http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-186093 Gareth Wed, 16 Sep 2009 07:25:56 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-186093 Strange my includes werent int he post I will try again in this post. If it doesnt work they are
hash_map and timeb.h

]]>
By: Gareth http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-186092 Gareth Wed, 16 Sep 2009 07:24:24 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-186092 I was curious about this, so I wrote this program. I added a million items into a hash_map
and found each item until I had done 32000000 finds and my results are very fast. I tried adding more into the map but I was running out of memory when I tried 32000000

time = 1253085626.187
time = 1253085626.661
time = 1253085629.317

This means finding 32000000 items took about 4 seconds. I would suggest you check your code again. Here is the source code, you may need to change the includes depending on your version of c++:
#include
#include

typedef __gnu_cxx::hash_map map_type;

int main(int argc, char** argv) {

typedef std::pair Int_Pair;

char buf [80];

map_type map;

struct timeb tp;
ftime(&tp);
printf(“time = %ld.%d\n”,tp.time,tp.millitm);

int i = 0;

for (i = 0; i<1000000; i++) {

map.insert(Int_Pair(i, i));

}

ftime(&tp);
printf("time = %ld.%d\n",tp.time,tp.millitm);

if (i != 1000000) {
printf ("Error!\n");
exit(1);
}

map_type::iterator j;

for (i = 0; i<32000000; i++) {
j = map.find(i%1000000);
}

ftime(&tp);
printf("time = %ld.%d\n",tp.time,tp.millitm);

if (i != 32000000) {
printf ("Error!\n");
exit(1);
}

return (EXIT_SUCCESS);
}

]]>
By: Gareth http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-186088 Gareth Wed, 16 Sep 2009 05:04:40 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-186088 Doesnt this mostly depend on the quality of the hash function ?

If you were using the default then that could be your problem. It mostly depends on your data…

http://www.ddj.com/architect/184405046?pgno=1

]]>
By: Thomas http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-185123 Thomas Wed, 26 Aug 2009 08:23:06 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-185123 I can believe this. About a year ago, I was doing a programming assignment for my university course (a Runge Kutta algorithm if I remember correctly), and for fun I decided to implement it in C, then C++ STL, then Python. I was surprised to find the C++ version took much, much longer to run than the python version.

]]>
By: David http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-82901 David Thu, 24 Jul 2008 20:18:41 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-82901 In addition to Priyendra’s comments, using the operator[] syntax in hash_map results in 2 copy-constructions of your object because of the way it is defined. For efficiency, insert() and find() should always be used

]]>
By: Priyendra Deshwal http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-2040 Priyendra Deshwal Fri, 20 Oct 2006 17:08:57 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-2040 So I assume you used the STL hash_map right. Did you have something like:

hash_map map;

// insertion
loop over millions of (k, v) pairs:
map.insert(k, v); // this will compute the hash value for k
// and *also* make a copy of v to put into
// the map.

// lookups
loop over millions of key values k:
ValType v = map[k]; // This copies the value v over
// from the container to the local variable v

So if the object which is being put into the table is largish (say c++ strings or any aggregate object) then it would be significantly faster to store pointers to the objects in the hashtable rather than the objects themselves. Alternatively, you could change the lookup to something like:

const ValType& v = map[k];

which will also get rid of the copies. Of course if you are storing integers or things of that scale only then this comment doesn’t really apply.

]]>
By: Diwaker Gupta http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-2039 Diwaker Gupta Fri, 20 Oct 2006 16:30:19 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-2039 Clearly some clarifications seem to be due! :-) I don’t mean to dismiss STL on the basis of just one experiment, but regardless I was surprised at how slow it was for this particular one. One important detail that I didn’t mention was that before each hash table lookup, we were doing some file I/O — so its not like the entire data was already in memory. Though the files we were reading were not big at all, and would possibly have been pulled into the RAM in the first few reads.

*@priyendra*: I’m not sure I follow. In our code we just have one or two instances of the hash table object. So why would the copy constructor be a problem? We’re not creating millions of instances of new objects. Am I missing something?

*@shashikant*: I’m not in a position to argue the statement “STL can’t be slower than Java” either way but I’ll take your word for it :-) I’m just as baffled by the results — maybe I’ll post the code shortly so that you guys can check it out yourself.

]]>
By: Shashikant http://floatingsun.net/2006/10/19/stl-is-slow-template-library/#comment-2037 Shashikant Fri, 20 Oct 2006 13:13:45 +0000 http://floatingsun.net/blog/2006/10/19/760/#comment-2037 Doesn’t look right. STL can’t be slower than Java. In Java, I have done hundreds of millions hash look-ups with hash size of size closer to million. I never came across a situation of hash lookup to be bottleneck in spite of the popular perception that Java is slower.

The only case when I think hash look-ups may take such times is load factor to be closer to 1.0, where look-up can degenerate into O(n).

]]>