Skip to content

Instantly share code, notes, and snippets.

@inactivist
Last active December 16, 2015 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inactivist/5363910 to your computer and use it in GitHub Desktop.
Save inactivist/5363910 to your computer and use it in GitHub Desktop.
"Quick and dirty" Python script for counting the lines per second pulled from stdin.
#!/usr/bin/python
"""Print stats about stdin per-line timings."""
import signal
import sys
import time
start = time.time()
count = 0
try:
for line in sys.stdin:
count += 1
except KeyboardInterrupt:
print
pass
end = time.time()
et = end - start
lps = count / et
print "Elapsed time = %f, lines = %d, lps = %f" % (et, count, lps)
@oddskool
Copy link

good !

what about this version:

#!/usr/bin/python
"""Print stats about stdin per-line timings."""
import sys
import time

max_secs_print = sys.argv[1] if len(sys.argv) > 1 else 5
max_lines_print = sys.argv[2] if len(sys.argv) > 2 else 1000

def lps(start, count):
    return count / float(time.time() - start)

start = time.time()
count = 0
try:
    for _line in sys.stdin:
        count += 1
        if not count % max_lines_print or (time.time() - start) > max_secs_print:
            print "lines = %d,  lps = %.2f" % (count, lps(start, count))
            count = 0
            start = time.time()
except KeyboardInterrupt:
    print "lines = %d,  lps = %.2f" % (count, lps(start, count))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment