home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1954 < prev    next >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: tchrist@convex.com (Tom Christiansen)
  2. Newsgroups: comp.lang.c,comp.lang.perl,alt.sources
  3. Subject: funcsize: determine size cost of a C function
  4. Message-ID: <107164@convex.convex.com>
  5. Date: 14 Oct 90 03:32:55 GMT
  6.  
  7. I was wondering how much certain C functions cost in terms of executable
  8. size, so I wrote this quick little script and got some interesting results.
  9. cuserid() ended up costing the most in my libc, since it calls getpw*() and
  10. ends up sucking in all the YP foo).  Some of the graphics stuff was
  11. staggering: the XmCommandError example listed below yielded 1.4 *megabytes*!
  12.  
  13. --tom
  14.  
  15. #!/usr/bin/perl
  16. #
  17. # funcsize - find a function's size overhead
  18. # tom christiansen, 13-oct-90
  19. #
  20. # USAGE: funcsize [-lxxx ...] [function ...]
  21. #
  22. # default library is libc
  23. # default function is all in first library
  24. #
  25. # functions that say they're size 0 got included by crt0,
  26. # or you've got shared libraries.
  27. #
  28. # EXAMPLES:
  29. #    funcsize         
  30. #    funcsize gethostbyaddr getuid printf scanf
  31. #    funcsize -lX11
  32. #    funcsize -lm cos atan2 gamma
  33. #    funcsize -lXm -lXt -lMrm -lX11 XmCommandError
  34. #
  35. # PORTING CAVEATS:
  36. #    knows what nm output looks like
  37. #    knows where libraries live
  38. #
  39. ##################################################################
  40.  
  41. printf $mask = "%-30s %8d\n", "main", $main_size = &compile_size("");
  42.  
  43. push(@Libraries, shift)     while $ARGV[0] =~ /^-l/;
  44.  
  45. (@Functions = @ARGV) || &load_library();
  46.  
  47. for $fun (@Functions) {
  48.     next unless $bytes = &compile_size($fun);
  49.     printf $mask, $fun, $bytes - $main_size;
  50. }
  51.  
  52. exit 0;
  53.  
  54. # ------------------------------------------------------------------------
  55.  
  56. sub load_library {
  57.     local($lib) = ($Libraries[0] =~ /^-l(\w+)/)
  58.             ? "/usr/lib/lib$1.a"
  59.             : "/lib/libc.a";
  60.  
  61.     open(LIB, "nm $lib|");
  62.     while (<LIB>) {
  63.     # skip _ident functions, allow $ in idents
  64.     next unless /\s+T\s+_([^_][\w\$]+)/;
  65.     push(@Functions, $1);
  66.     }
  67.     close LIB;
  68.     die "bad close on nm $lib (status $?)" if $?;
  69. }
  70.  
  71. # ------------------------------------------------------------------------
  72.  
  73. sub compile_size {
  74.     local($fun)  = @_;
  75.     local($size) = 0;
  76.     local($TMP)  = "/tmp/cc-$$";
  77.  
  78.     open (TMP, ">$TMP.c")     || die "can't open $TMP.c: $!";
  79.     print TMP "main() {";
  80.     print TMP "int i = $fun();"    if $fun;
  81.     print TMP "}\n";
  82.     close TMP             || die "can't close $TMP: $!";
  83.  
  84.     print `cc $TMP.c -o $TMP @Libraries`;
  85.  
  86.     unless ($?) {
  87.     (($size) = (stat($TMP))[7]) || warn "can't stat: $TMP: $!\n";
  88.     }
  89.  
  90.     unlink "$TMP.c", "$TMP.o", $TMP;
  91.     return $size;
  92. }
  93.