¿Usar printf("algo") o system("echo algo") cuesta más poder de procesamiento?

This question pertains to ANSI C. I think that it should be a simple answer...so which of these methods is the fastest? It doesn't really have anything to do with my current projects but it just came to mind one day and I didn't see the answer right away on the web...

preguntado el 31 de julio de 12 a las 14:07

Creo que printf is faster. I'm trying to find a specific piece I recall, though. -

There is no belief required that calling printf is faster than asking for the standard library to invoke a new process which will then run a command in a shell...Not a Real Question. -

Depends if there's a \n at the end of the string or not. but printf is faster tahn system(echo) for sure. -

4 Respuestas

printf is by far preferable, by many orders of magnitude and in every sense. It is a library function that's part of the C standard, and it makes your own program print the desired output. system on the other hand launches a new, separate process (something that takes a comparable eternity), and you still have to hope that your shell actually provides an echo command that does the same thing.

La única cosa peor que llamar system on echo sería llamar system to invoke a compiler that compiles another program that contains printf and run that, I guess...

No use system.

Respondido 31 Jul 12, 14:07

printf is not only preferable, it is the only correct answer. - user7116

system spawns a shell to execute your command. This means that there is a fork()/exec() to spawn the shell and the shell in turn calls another fork()/exec() ejecutar echo. Por no mencionar el shell has to search the PATH por echo, pass around environment variables etc.

Terribly, terribly slower than a function call to printf. This is besides the fact that system may fail because there is no shell, or no echo.

Respondido 31 Jul 12, 14:07

It would has been better in this case to make comparison of what is comparable... you can call something like a printf in system(), and not only processor is involved in the fastness.

In some specific cases, i believe that it's not impossible that a call to printf would be slower than calling a new small process (but it suppose to own a bizarre configuration processor vs hdd/memory).

In the same process, using a system_call is the fastest way to write something... (without buffer issues).

In most unix system for instance, a write call (if the only need is to write a simple string) is the fastest way.

Respondido 31 Jul 12, 14:07

system may not be able to find the echo command/program ... or someone with knowledge of your code could install a malicious program named echo on his/her path. So, not only is system going to be orders of magnitude slower, it's a security risk.

If you find yourself in a situation where you have to use system, tiene que siempre fully qualify the command you're calling (e.g. /bin/echo or /usr/bin/echo).

But you should never need to use it. Anything that you can invoke an external program to do ... you should be able to find a library (or code with which you can build a library) that you can invoke from your program to do the same thing. And it will be much faster and safer that way.

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.