0

I have two commands A and B. I want to execute them together, but the only feasible way in linux is to use &. That is,

$ A &
$ B

I don't expect 0 delay two commands, but approximately, how much should I expect? In milliseconds, microseconds or nanoseconds range? Thanks.

3
  • Why don't you just try it? Run a command that ouput the current time in miliseconds to different file
    – Martheen
    Feb 11, 2014 at 4:26
  • 2
    Or try GNU Parallel to actually run them simultaneously gnu.org/software/parallel
    – Martheen
    Feb 11, 2014 at 4:29
  • @MartheenCahyaPaulo Outputting the file takes extra time
    – return 0
    Feb 11, 2014 at 4:33

2 Answers 2

1

Note that any answer to this question is going to be dependant on what exactly the system is doing at any given time. For example:

  • What kernel scheduler you're using (their latencies differ)
  • What processes are you're running (they all compete for time, and the scheduler may not choose you're processes as early as you'd like)

And probably a thousand other things. For this reason, run this test yourself. Also note that the following could be wrong in many subtle ways, since I'm not very knowledgable about getting the best performance possible out of bash (well, zsh strictly speaking, since that's my login shell).


With this in mind, I devised a simple way to test this. GNU date has a way to show nanoseconds (yes, you read that correctly). Thus, to get a bunch of data, all you need to do is:

get_starting_diff() {
    date +%N & date +%N
}

# 5,000 iterations is probably good enough statistically
for iteration in $(seq 1 5000); do
    get_starting_diff
done > times

This will (besides chewing up a lot of PIDs) produce 10,000 line file, with the timing information in it. You can then use the following Python (v3.4) script to get an idea of what you're average timing difference is:

def pairwise(iterator):
    "Takes an iterator and extracts sequential pairs from it"
    pair = []
    for item in iterator:
        pair.append(item)
        if len(pair) == 2:
            yield pair
            pair = []

diffs = []
with open('times') as data:
    for start1, start2 in pairwise(data):
        diffs.append(int(start2) - int(start1))

diffs.sort()
median = diffs[len(diffs) // 2]
mean = sum(diffs) / len(diffs)

print("The median difference is", median / 1000000, "milliseconds")
print("The mean difference is", mean / 1000000, "milliseconds")

On my (Ubuntu 13.10, Linux 3.12.0-997-generic) system, this gives me:

The median difference is 1.077483 milliseconds
The mean difference is 3.680928442211055 milliseconds
0

You're running parallel tasks. Make no assumptions about what order they will run in. They could run in any order. B could even run to completion before A starts. Then the next time you run it you could get completely different results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.