1

Does AWK uses a lot of processing power? If so, is there a better scripting language to do it? Or should I do it in C itself (where rest of my code is).

Evan Kroske
  • 4,506
  • 12
  • 40
  • 59
hari
  • 9,439
  • 27
  • 76
  • 110
  • what is it that you are doing with C , that you can't do with the shell or other programming language ? – ghostdog74 Jul 29 '10 at 00:08
  • Ohh, its a huge system written in C and changing it is not an option. – hari Jul 30 '10 at 21:33
  • Okay. I figured it out partially. The awk script calles 'date'. Which is taking a little long to respond. Thanks for all who helped. – hari Aug 04 '10 at 00:31

2 Answers2

3

Depends on what you're telling it to do. Most of the work is passed to the regexp engine, which should be similar, no matter what language you use.

Now if you're using an awk script from inside a C program, and you have the resources to just implement the functionality in C too, you're best off doing that. You'll avoid the process creation/termination + communication overhead (which may or may not be a big part of the performance hit you'll get).

For more information, tell us more about your script!

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Thanks for the reply, in this case: from a C file --> a shell script which calls --> an awk script - which keeps looking at a log file and as a new entry gets in the log file, it gets filtered by the script. If matches the filters set, gets printed in XML format. – hari Jul 28 '10 at 23:28
  • Assuming the log file isn't written to too often, this shouldn't matter one way or the other. And if you choose to include the awk script into your C code, you'll have to include the shell script too. It will obviously be faster, but most likely not by much, so you have to weigh cost (time) vs benefit (speed). – Blindy Jul 28 '10 at 23:31
  • Of course, having everything in one program helps minimize dependancy failures and makes distribution easier. Food for thought. – Blindy Jul 28 '10 at 23:32
  • What if the log is being written too often? Would AWK take too much of cpu then? and something being written in non-awk would be better then? – hari Jul 28 '10 at 23:41
  • I think anything anything under ~10 writes/sec on average is fine as long as your script is straighforward. Writing it in C would of course be faster but ymmv on how MUCH faster it would be. – Blindy Jul 29 '10 at 06:54
  • Thanks much Blindy for the help. Is there a way I can measure how much CPU is each part of script is taking up? I mean debugging down the AWK script at function or line level? – hari Jul 29 '10 at 17:03
  • "Most of the work is passed to the regexp engine, which should be similar, no matter what language you use.". Actually, no, [awk's regexp engine is way faster than regexp engine in Perl or any other popular programming language](http://swtch.com/~rsc/regexp/regexp1.html). – Konrad Borowski Jun 02 '12 at 08:55
0

If most of your code is in c, It is probably cleaner to use c to do your string processing rather than shelling out.

You can use PCRE directly in your program.

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168