diff --git a/CHANGES.md b/CHANGES.md index 7a36f2c..98b4308 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ===== + modifies region.dist.txt to contain the aggregate coverage of each window when -b (integer) is specified (otherwise region.dist.txt and global.disk.txt are identical with -b (integer) ) ++ improve speed by ~30% when using per-base output with better int2str method 0.2.8 ===== diff --git a/int2str.nim b/int2str.nim new file mode 100644 index 0000000..0182264 --- /dev/null +++ b/int2str.nim @@ -0,0 +1,95 @@ +import bitops + +#[ + +this intToStr is adpated from: https://github.com/miloyip/itoa-benchmark countlut method. + +https://github.com/miloyip/itoa-benchmark/blob/master/license.txt +Copyright (C) 2014 Milo Yip + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +]# + + +const gDigitsLut = [ + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' +]; + +const powers_of_10 = [ + 0'i32, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000] + +proc countdigits(value:int32): int {.inline.} = + result = (32 - countLeadingZeroBits(value or 1)) * 1233 shr 12 + result = result - int(value < powers_of_10[result]) + 1 + +proc fastIntToStr*(value:int32, outstr:var string) {.inline.} = + outstr.setLen(countdigits(value)) + var value = value + var L = outstr.high + while value >= 100: + let i = (value mod 100) shl 1 + value = int32(value / 100) + + outstr[L] = gDigitsLut[i + 1] + outstr[L-1] = gDigitsLut[i] + L -= 2 + if value < 10: + outstr[L] = (value + '0'.int).char + + else: + let i = value shl 1 + outstr[L] = gDigitsLut[i + 1] + outstr[L-1] = gDigitsLut[i] + + +when isMainModule: + + import times + import strutils + + var t0 = cpuTime() + var outstr = newString(10) + for i in 0'i32..200_000_000: + fastIntToStr(i, outstr) + #doAssert outstr == $i + echo cpuTime() - t0 + + t0 = cpuTime() + echo "only to 100m for $" + for i in 0'i32..100_000_000: + let outstr = intToStr(i) + echo cpuTime() - t0 diff --git a/mosdepth.nim b/mosdepth.nim index 3312ca2..4e50d43 100644 --- a/mosdepth.nim +++ b/mosdepth.nim @@ -1,5 +1,6 @@ import hts import tables +import ./int2str import strutils as S import algorithm as alg import sequtils as sequtils @@ -663,8 +664,19 @@ proc main(bam: hts.Bam, chrom: region_t, mapq: int, eflag: uint16, iflag: uint16 if tid == -2: discard fbase.write_interval(starget & "0\t" & intToStr(int(target.length)) & "\t0", target.name, 0, int(target.length)) else: + var line = newStringOfCap(32) + line.add(starget) + var start = newString(12) + var stop = newString(12) + var value = newString(8) for p in gen_depths(arr): - discard fbase.write_interval(starget & intToStr(p.start) & "\t" & intToStr(p.stop) & "\t" & intToStr(p.value), target.name, p.start, p.stop) + line.setLen(starget.len) + fastIntToStr(p.start.int32, start) + fastIntToStr(p.stop.int32, stop) + fastIntToStr(p.value.int32, value) + line.add(start); line.add('\t') + line.add(stop); line.add('\t'); line.add(value) + discard fbase.write_interval(line, target.name, p.start, p.stop) if quantize.len != 0: if tid == -2 and quantize[0] == 0: var lookup = make_lookup(quantize)