Producing the fastest possible executable

后端 未结 10 2097
滥情空心
滥情空心 2021-02-06 15:55

I have a very large program which I have been compiling under visual studio (v6 then migrated to 2008). I need the executable to run as fast as possible. The program spends most

相关标签:
10条回答
  • 2021-02-06 16:49

    I agree with what everyone has said about profiling. However you mention "integers of various sizes". If you are doing much arithmetic with mismatched integers a lot of time can be wasted in changing sizes, shorts to ints for example, when the expressions are evaluated.

    I'll throw in one more thing too. Probably the most significant optimisation is in choosing and implementing the best algorithm.

    0 讨论(0)
  • 2021-02-06 16:52

    You have three ways to speed up your application:

    1. Better algorithm - you've not specified the algorithm or the data types (is there an upper limit to integer size?) or what output you want.

    2. Macro parallelisation - split the task into chunks and give each chunk to a separate CPU, so, on a two core cpu divide the integer set into two sets and give half to each cpu. This depends on the algorithm you're using - not all algorithms can be processed like this.

    3. Micro parallelisation - this is like the above but uses SIMD. You can combine this with point 2 as well.

    0 讨论(0)
  • 2021-02-06 16:53

    You're asking which compiler options can help you speed up your program, but here's some general optimisation tips:

    1) Ensure your algorithms are appropriate for the job. No amount of fiddling with compiler options will help you if you write an O(shit squared) algorithm.

    2) There's no hard and fast rules for compiler options. Sometimes optimise for speed, sometimes optimise for size, and make sure you time the differences!

    3) Understand the platform you are working on. Understand how the caches for that CPU operate, and write code that specifically takes advantage of the hardware. Make sure you're not following pointers everywhere to get access to data which will thrash the cache. Understand the SIMD operations available to you and use the intrinsics rather than writing assembly. Only write assembly if the compiler is definitely not generating the right code (i.e. writing to uncached memory in bad ways). Make sure you use __restrict on pointers that will not alias. Some platforms prefer you to pass vector variables by value rather than by reference as they can sit in registers - I could go on with this but this should be enough to point you in the right direction!

    Hope this helps,

    -Tom

    0 讨论(0)
  • 2021-02-06 16:59

    Another optimization option to consider is optimizing for size. Sometimes size-optimized code can run faster than speed-optimized code due to better cache locality.

    Also, beyond optimization operations, run the code under a profiler and see where the bottlenecks are. Time spent with a good profiler can reap major dividends in performance (especially it if gives feedback on the cache-friendliness of your code).

    And ultimately, you'll probably never know what "as fast as possible" is. You'll eventually need to settle for "this is fast enough for our purposes".

    0 讨论(0)
提交回复
热议问题