2017-11-26 11 views
-1
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <chrono> 

template<class Resolution = std::chrono::milliseconds> 
class ExecutionTimer { 
public: 
    using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady, 
     std::chrono::high_resolution_clock, 
     std::chrono::steady_clock>; 
    ExecutionTimer() = default; 

    ~ExecutionTimer() { 
     std::cout 
      << "Elapsed: " 
      << std::chrono::duration_cast<Resolution>(Clock::now() - mStart).count() 
      << std::endl; 
    } 
private: 
    Clock::time_point mStart = Clock::now(); 
}; 

int main() { 
    ExecutionTimer<> timer; 

    std::vector<int> v(50000000); 
    std::sort(std::begin(v), std::end(v)); 
    return 0; 
} 

私は2つの異なるコンパイラでC++コードの上でコンパイルしようとしましたが、私は環境変数clとg ++コンパイラの違いは?

    「C使用して走った

    としての両方の.exeファイルの実行時間の違いを観察:\プログラムファイルを( x86の)\ \ 2017 \コミュニティ\ VC \ツールのMicrosoft Visual Studioのを\ MSVC \ 14.10.25017 \ binに\ Hostx64 \ x64" の


CL -> cl /ehsc Benchmark.cpp 
     364 (ms) 
g++ -> g++ -std=c++17 Benchmark.cpp -o bench17 
     16565 (ms) 

缶誰かがなぜ大きな違いがあるのか​​教えてください。

+5

最適化を有効にしないベンチマークは時間の無駄です。 g ++コマンドラインに '-O3'を、VC++コマンドラインに'/O2'を追加して実験を再開してください。 – Praetorian

+0

ありがとう、それでも私は9735(ms)を実行すると g ++ -O3 -std = C++ 17 BenchmarkTest.cpp -o benchO3。 –

+1

[このサイトにある](https://godbolt.org/)のコードを入れて、異なるコンパイラの出力の違いを確認してみてください – JLev

答えて

0

デフォルトでは、g ++は最適化を一切実行しません。これは、コンパイル済みの標準ライブラリの外ではほぼ完全に実装されている標準テンプレートを使用するコードでは特に問題になります。

私はテストするMicrosoftコンパイラがありませんが、私のシステムでは-O2を有効にすると実行時間が25秒以上から1秒未満に短縮されます。

関連する問題