Better HN
topnewbestaskshowjobs
Log inSign up

soundmasterj

6 karma · joined Sep 25, 2021 (5y ago) · 5 submissions

Submissions

comment 3y ago · link · parent

Oh no, when will the children sleep

comment 3y ago · link · parent

They're saying we can have the moon.

comment 5y ago · link · parent

This is good too. I would use a generator expression instead of the map though probably.

comment 5y ago · link · parent

I didn't try to write optimized code, but idiomatic Python. Which also happens to be 10x faster.

To make it streaming, take the second version and remove the readlines (directly iterate over f).

Delegating work to Python's C internals is fine IMO because "batteries included" is a key feature of Python. "Nim outperforms unidiomatic Python that deliberately ignores key language features" is perhaps true, but less flashy of a headline.

And to be honest, I mainly wrote this because the other top level Python implementations for this one were terrible at the time of the post.

comment 5y ago · link · parent

This is reasonably idiomatic Python and 10x faster than the implementation in the original post:

  with open("orthocoronavirinae.fasta") as f:
      text = ''.join((line.rstrip() for line in f.readlines() if not line.startswith('>')))
      gc = text.count('G') + text.count('C')
      total = len(text)


Or if you want to be explicit, this is just as fast (and might scale better for particularly long genomes):

  gc = 0
  total = 0
  
  with open("orthocoronavirinae.fasta") as f:
      for line in f.readlines():
          if not line.startswith('>'):
              line = line.rstrip()
              gc += line.count('C') + line.count('G')
              total += len(line)

I didn't test Nim but the author reports Nim is 30x faster than his Python implementation, so mine would be about 3x slower than his Nim.