[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] true,false: avoid initialization overhead unless needed
From: |
Bernhard Voelker |
Subject: |
[PATCH] true,false: avoid initialization overhead unless needed |
Date: |
Mon, 07 Jul 2014 10:48:19 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 |
This is a minor performance improvement:
$ strace -fv /bin/true --xxx 2>&1 | wc -l
105
$ strace -fv src/true --xxx 2>&1 | wc -l
26
$ time for i in $(seq 1000) ; do /bin/true --xxx ; done >/dev/null
real 0m5.083s
user 0m0.481s
sys 0m4.319s
$ time for i in $(seq 1000) ; do src/true --xxx ; done >/dev/null
real 0m4.729s
user 0m0.319s
sys 0m4.107s
Do you think it's worth doing?
Have a nice day,
Berny
From 1c550664611f7e2557925898b1c896952675dbda Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <address@hidden>
Date: Mon, 7 Jul 2014 10:31:46 +0200
Subject: [PATCH] true,false: avoid initialization overhead unless needed
* src/true.c (main): Instead of generally doing the common program
initialization when argc equals 2, only do it when one of the
--help or --version option is specified.
---
src/true.c | 36 +++++++++++++++++++++++-------------
1 file changed, 23 insertions(+), 13 deletions(-)
diff --git a/src/true.c b/src/true.c
index 8a1c4f9..0555721 100644
--- a/src/true.c
+++ b/src/true.c
@@ -58,22 +58,32 @@ main (int argc, char **argv)
argument. */
if (argc == 2)
{
- initialize_main (&argc, &argv);
- set_program_name (argv[0]);
- setlocale (LC_ALL, "");
- bindtextdomain (PACKAGE, LOCALEDIR);
- textdomain (PACKAGE);
+ enum {NONE, H, V};
+ short opt = NONE;
+ if (STREQ (argv[1], "--help"))
+ opt = H;
+ else if (STREQ (argv[1], "--version"))
+ opt = V;
- /* Note true(1) will return EXIT_FAILURE in the
- edge case where writes fail with GNU specific options. */
- atexit (close_stdout);
+ if (opt != NONE)
+ {
+ initialize_main (&argc, &argv);
+ set_program_name (argv[0]);
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEDIR);
+ textdomain (PACKAGE);
- if (STREQ (argv[1], "--help"))
- usage (EXIT_STATUS);
+ /* Note true(1) will return EXIT_FAILURE in the
+ edge case where writes fail with GNU specific options. */
+ atexit (close_stdout);
+
+ if (opt == H)
+ usage (EXIT_STATUS);
- if (STREQ (argv[1], "--version"))
- version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
- (char *) NULL);
+ if (opt == V)
+ version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+ (char *) NULL);
+ }
}
exit (EXIT_STATUS);
--
1.8.4.2
- [PATCH] true,false: avoid initialization overhead unless needed,
Bernhard Voelker <=