optimizing my code (memory usage, postfix and prefix notation, "continue")

Started by
11 comments, last by Ravyne 11 years ago

Empty braces might invite the thought that the programmer forgot to fill in the loop body, and an empty statement (a single semicolon) is first of all very easy to miss, and causes really strange errors if you should ever forget or mistakenly delete it.

I pretty much never find myself needing a loop without a body, but when I do, I put a single semicolon indented on the next line. It stands out pretty well and doesn't look like you forgot to write a body.
Advertisement

Okay, thanks everyone for the replies (and sorry for my late reply).
It seems that I should have included my entire function to prevent confusion here, as some people don't really know what I want to archieve here.

So: the object of this function was to split a string on a character. However, the function that Ravyne showed here (yes, I know there are even more possible solutions) doesn't really do what I want, as I have a few more requirements xd. That function was actually one of the first I came up with.
Let me explain you a few other things.
- If the string doesn't contain that character, the function stores the entire string in a vector (size 1)
-if the string is empty, the function stores an empty string ("") in a vector (also size 1)
-If the character occurs as first character an empty string will be stored as first element in the vector
-If there are multiple duplicates of that character following, it ignores all those.

An example:
vector<string> splittedString;
char splitChar = '.';
string stringToSplit = ".test...12..3.";

After the instruction
SplitLine(stringToSplit, splittedString, splitChar);
splittedString contains the strings
0. (empty)
1. test
2. 12

3. 3
4. (empty)

Not trying to argue, just saying how it is... Like wintertime said, those two loops don't exactly do the same thing, but they do if you actually see the entire function. As I said before, I've tested all the possibilities comprehensively. So, without further ado, my entire function. You can use all my possibilities, they all do the same thing.












inline void DaeToAniConverter::SplitLine(string line, vector<string>& splittedline, char splitchar)
{
	//vector with string segments should contain at least one string
	if (line == "")
	{
		splittedline.push_back("");
		return;
	}
        int ll = line.length();
	splittedline.clear();
	int prevIndex(0);
	int  j(0);
	while(j < ll)
	{
		int tempJ = line.find_first_of(splitchar, j);
		j = tempJ >= 0 ? tempJ : ll;
		//skip multiple equal characters
		splittedline.push_back(line.substr(prevIndex, (j-prevIndex)));
		while (j < ll - 1 && line[++j] == splitchar)
		{}
		prevIndex = j;
	}

}

As you can see here, the main loop is only executed as many times as there will be string segments (in my last example, 5 times), so not character per character. I actually do use line.find_first_of.

And about that performance issue, you are all right smile.png. It really doesn't make a big difference, and a good structure is sometimes more important than performance.
Thanks to everyone else for your constructive remarks.
Again (but now you know what I want to archieve here), if there is a better way to archieve this functionctionality, I would gladly hear about it.

I'm just a 19 year old student, programming in my free time. Everyone makes mistakes, right? smile.png

gz
Nick

Edit: This may be important. This function is part of a program that reads a file, converts it to a custom format, and writes it to a new file. I'm talking about pretty big files, so this function is usually executed at least hundred times (usually a few hundred). Right now it only takes 2-5 seconds for the program to do its thing, so that's not too bad smile.png.

Let me explain you a few other things.

  1. If the string doesn't contain that character, the function stores the entire string in a vector (size 1)
  2. If the string is empty, the function stores an empty string ("") in a vector (also size 1)
  3. If the character occurs as first character an empty string will be stored as first element in the vector
  4. If there are multiple duplicates of that character following, it ignores all those.

The second rule is redundant. An empty string won't contain the character, and therefore rule 1 applies, and since the string is empty, rule 1 will store an empty string.

I get why you want to eat multiple occurring characters, but I don't get why you want an empty string if its the first character (and last, by your example?)

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement