I can’t give you the code because this is work related, and I couldn’t make a minimal code that mimics the behavior.

My of my programs at work is a log parser, it reads syslog and use compiled regex pattern to match syslog, the whole program looks like this

If match := pattern.match(line):
  Do this
elif match := pattern2.match(line):
  Do that
…

During almost two years of me developing the programs, there are more patterns and more things to do and it gets slower.

But I recently figure out that if I commented out Do that and replace it with pass, my program speeds up, even if pattern2 never matches in my test case.

More strange thing is that in my attempts to use cProfile to profile things, my program runs 2.5x to 3x faster by just doing

from cProfile import Profile
with Profile() as profile:
  main()

I don’t even call anything with profile variable and it speeds up my program, why is that?

  • CameronDev
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    No idea about the profile thing, but the elif cond: pass thing could be some form of optimisation? No need to evaluate the elif condiition if its not gonna do anything anyway?