开发者

Is there a tool to test the conciseness of c program? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_运维百科

Closed 6 years ago.

Improve this question

For example I want to check whether the following code can be more concise or not:

for(i = 0; i < map->size; i++){
    if(0 < map->bucket[i].n){
        p = map->bucket[i].list;
        while(p){
            h = hash(p->key) % n;
            if(bucket[h].list){
                new_p = bucket[h].list;
                while(new_p->next)new_p = new_p->next;
                new_p->next = p;
                next = p->next;
                p->next = NULL;
                p = p->next;
            }
            else{
                bucket[h].list = p;
                bucket[h].n++;
                next = p->next;
                p->next = NULL;
                p = p->next;
            }   
        }
    }
}

Is there any tool for such kind of task?

It would be of great help to me .


You could be asking for several things here. One possible interpretation of your question is that you are looking for a slicer, a tool that takes a program and produces a program made of a selection of the instructions from the original program, and that computes some or all of the results of the original. In other words, a slicer removes that instructions that are not necessary for at least one of the results you are interested in.

There is an Open-Source slicer for C programs here.

For making programs more concise, perhaps the constraint of keeping only instructions from the original program as they are is too strong. You may also want to allow some transformations other than "keeping" or "removing". The framework that the slicer linked above is part of provides this kind of transformation too. For instance:

int main(int c, char **v)
{
  int x = 2;
  int y = x + c;
  return y; 
}

In the above program, a slicer instructed to preserve the exit code cannot remove any instruction: they all contribute to the result. But if you first apply a constant propagation, transforming every constant expression in its value:

int main(int c, char **v)
{
  int x = 2;
  int y = 2 + c;
  return y; 
}

Then a slicer can in a second pass remove the useless variable x:

int main(int c, char **v)
{
  int y = 2 + c;
  return y; 
}


The answer is a qualified "no",

  • No, because it is impossible to write a program that will always answer whether code can be made more concise or not. If such a program could exist, you could use it to create a logical paradox (which is why we know it can't exist).

  • Programs of this nature don't generally exist, because making source code smaller in a mechanical way makes the code less readable. Most programmers just don't see any benefit in making the C code as small as possible.

  • Yes, because you can use static analysis to find dead code.

People put enormous amounts of research into making programs smaller and faster automatically, using static analysis, but nearly all of that research gets applied to making better optimizers for compilers. Optimizers can produce almost unreadable output, which is why we don't use them to produce source code but object code only.

Keep your source code clean and readable. The compiler will take care of the rest, 99% of the time.


You could try compiling it with optimizations and looking at the optimized assembly code. But then you really aren't gaining anything in terms of speed since the compiler optimized away all the extra stuff anyway. All you gain is an increase in readability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜