Quick find and replace on strings

Started by
22 comments, last by emileej 20 years, 5 months ago
quote:Original post by Ready4Dis


Ahh, but I wrote my own string class that I use in my game engine. The buffer allocates extra space for when adding stuff into the string, thereby not needing to re-allocate the buffer each time it's added to . So, i am uber elite



I have to agree with antareus and Fruny here.

The above quote just proves their point even further, std::string already gives you the ability to reserve memory.

std::string example;example.reserve(1000); //reserve memory for 1000 characters   


You could also just write another allocator for std::basic_string to allocate more memory than needed (although most implementations of the standard allocator already do this anyway).

Its just another example of over optimization gone crazy, writing your own string class to do something you could do very simply with std::basic_string, which is written and tested by the most experienced C++ programmers in the world.

quote:
Yes, for this simply filling it in with a trivial std::string::find/replace would have been extremely simplistic, but later if it wasn't fast enough, he'd have to go through his program and change over all the std::strings with char arrays, or a custom string class, change all function prototypes, replace any function that he used strings in, etc. It'd be twice as much work replacing all that crap as it would to just write it with speed in mind in the first place.


Or a far more likely situation would be that you end up writing your own string class (ready4dis::string) and using that instead, only to find that it is full of bugs and causing serious problems in your software. You then have to go back and replace every ready4dis::string with std::basic_string.



[edited by - Jingo on November 15, 2003 12:44:31 PM]
Advertisement
One of the popular string search algorithms is the Boyer-Moore algorithm. It is an older algorithm and as a consequence, there are a few variations that exists. It is fast and easy to implement. I didn''t check out the link provided earlier, so you may have already come across this method.

Mike
quote:The ones saying not to optimize prematurely can''t differentiate the two, and just tell everyone not to optimize.

I''m sure Knuth would love to hear that.

Optimization implies something exists. Many many threads are about optimizing something in a product that isn''t even functional. I really don''t see whats so wrong with doing the simplest thing that works (string class over char*) and then going back with a profiler and hitting the areas where it really hurts.

Programmers usually have a decent idea what area of the code gets used the most. Nothing wrong with tweaking, but it seems like people overlook the fact that the greatest gains are made by making algorithmic, rather than silly changes like bit-shifting or using char* everywhere because you''re scared of std::string. This isn''t an excuse to write sloppy, slow code, its more of people seem to enjoy introducing additional complexity into their programs based on ancient optimization principles.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
That''s because optimization is "cool." Nevermind the fact that a working program is cooler.

Don''t use std::string, char* is faster!
Don''t use std::vector, new int[] is faster!
Don''t use std::sort, qsort is faster!
Don''t write a game, it''s more fun to bicker over whether
if(a && b) 

or
if(a) if(b) 

is faster!
Don''t listen to respected posters, the guy that agrees with you is right!

/sarcasm

This topic is closed to new replies.

Advertisement