tag:blogger.com,1999:blog-7764126179108633515.post4573019703104503769..comments2023-11-26T05:25:06.785+03:00Comments on Ilya Martynov's blog: STL strings vs C strings for parsingIlya Martynovhttp://www.blogger.com/profile/03019237563335985484noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7764126179108633515.post-79284157666869723152008-07-21T03:03:00.001+04:002008-07-21T03:03:00.001+04:00cc &&cc && <a href="http://rdd.su" title="хеви метал."> </a>jilcovhttps://www.blogger.com/profile/13527480015850637359noreply@blogger.comtag:blogger.com,1999:blog-7764126179108633515.post-81952832945798041172007-12-07T18:26:00.000+03:002007-12-07T18:26:00.000+03:00You can do it with std::string, you just have to w...<I>You can do it with std::string, you just have to work with iterator ranges instead of copied substrings.</I><BR/><BR/>I tried do that but while doable it generally makes code more complex as you have to keep two variables to represent the range instead of one for C string. Or am I missing some standard way to represent the range as a single variable? I guess if there was a class with interface similar to string's interface which allows to work on part of other string that would simplify code a lot.<BR/><BR/>As for safety: my impression that once your are using iterators it is not that much more safer then raw pointers. Standard library doesn't protect you if you ++iterator beyond end() - it is up to programmer to write the code correctly.Ilya Martynovhttps://www.blogger.com/profile/03019237563335985484noreply@blogger.comtag:blogger.com,1999:blog-7764126179108633515.post-25681367528336661822007-12-07T17:55:00.000+03:002007-12-07T17:55:00.000+03:00You can do it with std::string, you just have to w...You can do it with std::string, you just have to work with iterator ranges instead of copied substrings.<BR/><BR/>If you use typical C idioms (e.g. strchr and pointer arithmetic) then the plain C version probably will be faster. However, C++ has its own idioms that might perform better (where that might mean faster, or safer, or both)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7764126179108633515.post-3465247936312944562007-12-06T18:56:00.000+03:002007-12-06T18:56:00.000+03:00You should try it with str1 pointing to user-suppl...<I>You should try it with str1 pointing to user-supplied data (e.g. read from stdin or passed in via argv) for a more realistic benchmark.</I><BR/><BR/>Thanks for hint. I just did that (passed the string via arvs) and it does change the result. Though C strings are still faster (~10%) but not as much:<BR/><BR/>ilya@denmark:~$ g++ -O3 ./test.cc && ./a.out ' a '<BR/>Total time: 9.14 sec<BR/>Iterations: 500000000 it<BR/>Time per iteration: 1.828e-05 msec<BR/>Rate: 5.47046e+07 it/sec<BR/>Total time: 10.57 sec<BR/>Iterations: 500000000 it<BR/>Time per iteration: 2.114e-05 msec<BR/>Rate: 4.73037e+07 it/sec<BR/><BR/>Meanwhile I rewrote my URL parser using C strings and it is now 2x times faster. I guess the speedup comes from the fact that with C strings I can minimize number memory allocations for temporary strings. With C string if you want to take substring of another string you can just take pointer in the middle of the original and place '\0' where substring should end if don't mind destroying the original string. With STL (ok, standart) strings you cannot do this and have to make copies.Ilya Martynovhttps://www.blogger.com/profile/03019237563335985484noreply@blogger.comtag:blogger.com,1999:blog-7764126179108633515.post-7277199242530568432007-12-06T18:17:00.000+03:002007-12-06T18:17:00.000+03:00Looks as though GCC is replacing the call to strch...Looks as though GCC is replacing the call to strchr with a compiler builtin, and noticing that str1 points to a literal and doing the search at compile-time.<BR/><BR/>Unless all the URLs you're parsing are known at compile-time you should try it with str1 pointing to user-supplied data (e.g. read from stdin or passed in via argv) for a more realistic benchmark.<BR/><BR/>You might be surprised at the result.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7764126179108633515.post-34996721176862738982007-12-06T18:05:00.000+03:002007-12-06T18:05:00.000+03:00I do wish people would stop saying STL when they m...I do wish people would stop saying STL when they mean Standard Library.Anonymousnoreply@blogger.com