How efficient is this code?

This is a discussion on How efficient is this code? within the C Programming forums, part of the General Programming Boards category; Below is a function which takes a string and an integer as it's parameters, and returns a string. It's purpose ...

How efficient is this code?

Below is a function which takes a string and an integer as it's parameters, and returns a string. It's purpose is to remove the character at the position denoted by the integer, and return the remaining characters in the form of another string.

The calling function ensures that the value of (int letter) always relates to a valid position in (char *word).

The problem I've got is that this function is being called many many times during the excution of the program (potentially hundreds of millions of times), and I need to make sure that it is as fast as possible. Can anyone suggest a way of improving the speed of this function?

Also, I'm just guessing that this function might be a bottleneck, as I don't know how to work out what percentage of time the program takes to execute each part. Does anyone know of a way to measure this?

>Can anyone suggest a way of improving the speed of this function?
Don't bother until you're sure that there's a performance problem. Optimizing this function would probably result in it being harder to understand. However, one quick and easy way to make this function faster while also making it safer is to avoid dynamic allocation by passing in a buffer for the new word:

This way you don't have the expensive call to malloc, you don't call strlen with every iteration of the second loop, and you don't have to worry about releasing the memory later in the program.

>I'm just guessing that this function might be a bottleneck
It's been repeatedly shown that programmers are notoriously bad at guessing where bottlenecks are.

>Does anyone know of a way to measure this?
The best way is to use a profiler to get percentages during execution.

>str = (char *) malloc( strlen( word ) - 1);
You don't have to cast malloc in C, it's also considered a bad thing because it can hide the error of not including stdlib.h. Also, since the size of word is strlen(word) + 1, the correct size after removing a single character would be strlen(word), as it is you have an off-by-one error that may or may not cause problems later. Probably when you call free.

>for( ctr = letter ; ctr < strlen( word ) ; ctr++ )
It's not a good idea to include function calls as counting loop conditions if you can avoid it easily. This loop would be slowed considerably by the repeated overhead of calling strlen and the time it takes to find the length of word. A better way would be to either work with nul characters as strlen probably does, or call strlen and save the value:

If the function takes (char *new_word) as a parameter, do you still need to (return new_word? Wouldn't it be the same to declare the function as void, and just alter the value of *new_word? Also, are you implying that the malloc for the new_word is placed in the calling function, or is there some way of avoiding the malloc entirely, even though the length of (char *word) is not known until run-time?

>Is there any reason that you chose to use size_t as opposed to int?
The function doesn't work with negative indices and size_t is to be preferred over int for array indicies if you can manage it.

> do you still need to (return new_word)?
No, but it makes setting a pointer to the beginning of the array slightly simpler. You can do

Code:

char* p;
...
p = extractLetter(word, new_word, i);

instead of

Code:

char* p;
...
extractLetter(word, new_word, i);
p = new_word;

It can also be used for error checking. If something goes wrong you can return NULL. You'll notice that fgets does the same thing; the buffer doesn't need to be returned, but doing so adds a lot of useful flexibility to the function.

>Also, are you implying that the malloc for the new_word is placed
>in the calling function, or is there some way of avoiding the
>malloc entirely, even though the length of (char *word) is not known until run-time?
If the length of word isn't known until run-time and you can't determine a safe upper limit, you'll need to use malloc. However, by placing the onus of allocating memory on the calling function you can implement useful optimizations such as only calling a memory allocation routine if the current size of word is longer than the new_word buffer. This wasn't possible with your previous function.

>By the way, could you recommend a profiler for use on Solaris?
Take a look at prof in your man pages.

I've just run prof against the program, and found that over 50% of the run-time is taken up by a process called _brk_unlocked. The 'largest' user function only takes 10% of run-time.

I've tried searching google for _brk_unlocked, but not found much of use. I gather it's something to do with memory allocation for variables, but can anybody shed any more light? ...and is there anything I can do to improve it?

Sorry, I had not included the code for the append_perm function, and it is that which contains the calls to malloc(). I recompiled the code with append_perm rem'd out, and nearly all of the calls to _brk_unlocked disappeared.

I have attached the code files - the intention is to generate a list of every permutation of a string supplied at the command line. I thought I had come up with quite a neat little recursive function to do this, but I'm struggling to keep the run-time down if the string length exceeds 10 characters.

If anyone is interested in trying to improve the code, please let me know your ideas. Also, do you know of a more efficient approach to permutation generation?