A better way to do this would be to have each of the five bwa mem commands running simultaneously instead of one after the other. As we discussed in Bioinformatics Club, one way to do this is by appending an & character to the end of the command. Here is what that looks like:

for samplename in "SRR518710" "SRR518711" "SRR518712" "SRR518713" "SRR518717"
do
bwa mem arctos.fna.gz ${samplename}_1.fastq ${samplename}_2.fastq > test_${samplename}.mapped.sam &
done
 

This speeds things up significantly. It took an average of 27 minutes, 1.8 seconds to run this script, which is about a 13% decrease in runtime (Memory use was much higher than the first method: 95% CPU efficiency, 66% memory efficiency).

However, we can make this even faster using the parallel utility. Here is how that looks:

module load parallelgnu
parallel "bwa mem arctos.fna.gz {}_1.fastq {}_2.fastq > test_{}.mapped.sam" \
::: "SRR518710" "SRR518711" "SRR518712" "SRR518713" "SRR518717"

Using this utility, it took me an average of 24 minutes, 11 seconds to run this script. That is another 10% decrease in runtime (22% decrease in total compared to the first method). Plus, in my opinion, the code ends up a bit cleaner. As an added bonus, memory usage is lower using this method than the second method using & (27% memory efficiency, 94% CPU efficiency).

Here is an article that explains what the parallel utility is doing and why it is so much faster: https://www.biostars.org/p/63816/