5

In awk there are two output functions: print and printf.

  • Are their implementations in awk very different?
  • What are the differences regarding performance/speed (if possible — theoretical, not only with "time" on command line)?
  • Do they use the same system calls?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
static
  • 8,126
  • 15
  • 63
  • 89

1 Answers1

13
  1. The performance difference is probably not measurable.
  2. The print function outputs a newline at the end; printf does not unless requested.
  3. The print code converts the arguments to strings and then sends them to the output separated by the OFS (output field separator).
  4. The printf code might need to convert the string to a double before formatting it using a double format (%16.8g or something), and similar operations.
  5. The system call used is going to write(2) or something similar for both, but there'll be code (probably <stdio.h>) layered above that.

All that adds up to:

  • The implementations are different; print is a little simpler (and therefore faster) than printf.
  • The difference is probably not measurable for most purposes.
  • Use print if it will do what you need; use printf when it does what you need.
  • Don't worry about it.

And using a sprintf followed by print is likely to be slower than using printf directly, so don't.

In case of doubt, measure.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • thank you! I measured time for a specific output line with 3 FP parameters (many times for statistics): `print:printf ~= 16s:17s`. So the difference is realy not so big. – static Sep 06 '12 at 23:15