Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Performance of hash_set vs. Java
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Tom Widmer, post: 1514050"] Ok, I'm 95% sure that gcc 3.3 uses a copy-on-write (COW) string implementation, with reference counting (gcc 3.4 certainly does). This means that copying a string doesn't require that any memory is allocated - the copy shares representation with the original and only creates its own unique copy when it might modify it. This also means that copying strings is cheap, as long as those strings are only accessed through const member functions after the copy has occurred. You definitely want: lines.reserve(estimateOfNumberOfLines); line.reserve(enoughForALine); //may well help The above line is going to be slow, since it involves copying the whole vector. Instead, you might do: //avoid coping the vector: lines.resize(lines.size() + 1); //add extra default element lines.back().swap(thisLine); //swap it with the current element words.reserve(50); //say That might be slightly more efficient as: words.push_back(std::string(line, firstMark, lastMark - firstMark)); but I doubt it will make much difference with a COW string implementation. See above. It might not be counting time spent in system functions, such as IO (system vs user time?). If you want to optimize the above, I'd do this: Write a simple immutable string type that is initialized with a pointer and a length, but allocate no memory and does nothing in the destructor. Include in the string a cached hashcode value, so that the hashcode need only be calculated once. e.g. class mystring { char const* m_ptr; std::size_t m_length; mutable unsigned long m_hashCode; public: mystring(char const* ptr, std::size_t length) :m_ptr(ptr), m_length(length), m_hashCode(static_cast<unsigned long>(-1)) { } unsigned long hashCode() const { if (m_hashCode == static_cast<unsigned long>(-1)) { //calculate hashCode (copy java.lang.String code?) } return m_hashCode; } char operator[](std::size_t index) const { return m_ptr[index]; } //operator==, <, etc. //compiler generated destructor, copy, assignment are fine. }; Read the entire file into a vector<char>. Iterate over the vector, adding creating "mystring"s pointing into the vector for each word. You might also consider replacing ' ' characters with '\0's, so that you can add c_str() method to mystring that simply looks like this: char const* c_str() const { return m_ptr; } Operate on this new vector<vector<mystring> >. That should be much much faster, since the memory allocation overhead will be vastly decreased. If you don't need a vector of lines, just have an overall vector of words for another speed up. Essentially, optimization in non-numerical C++ is often about reducing the number of calls to "new" and "delete", which are often even slower than the Java versions (new + gc). Tom [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Performance of hash_set vs. Java
Top