Performance tips

来源:互联网 发布:算法 第4版 豆瓣 编辑:程序博客网 时间:2024/05/17 06:39

TIPS


  • Remember the 80/20 rule
    • 80% of a program spends its time in 20% of the code
  • Prefer x += y, to x = x + y
  • Use pre-incrementation (++i), instead of post-incrementation (i++)
    • This typically applies to any object that has a custom incrementor/decrementor instead of a native type.
  • Pre-allocatation of memory will help memory fragmentation
  • Don't use dynamic_cast, its a run-time cast operator
  • Avoid using printf (printf, sprintf, strftime) functions where alternatives are possible
  • Evaluate once, use many times
  • In loops if a value doesn't change, evaluate it outside the loop ( don't trust the compiler to optimise )
  • Use Just-In-Time (lazy) evaluation where appropriate
    • Don't evaluate something if it will won't be used
    • i.e. evaluate just before using
  • Use Object Caching to pre-allocate memory and objects
    • i.e. by overriding new and delete operators
    • Helps not only in performance but also memory fragementation
    • Helps greatly in a system with various different sizes of objects
  • Use reference-counting and smart-pointers to avoid excessive memory allocation
  • Initialise objects instead of assignement
    • Don't use 'int a; a = 2;'
    • Use 'int a = 2;'
  • MEB Specific
    • There are several system calls made every time round each MEB main loop.
    • The MEB is single threaded, hence end->end time is just as important as time of individual components
  • System calls
    • system() calls are really expensive. If called frequently look into alternatives
    • sigprocmask is really bad on Linux/gcc, similarly gettimeofday is very expensive as well
    • iostat again is really expensive on Linux/gcc
    • Look into alternatives into all system calls.
  • Pcap Issues
    • Standard implementations usually incur a high overhead due to multiple copies of the data
    • Look into alternatives, e.g. PF-RING, etc
原创粉丝点击