Neodym's blog

By Neodym, 11 years ago, In Russian

Подскажите пожалуйста, как подключать таймеры в языке С++(Чтобы можно было определять, сколько времени прошло с начала работы программы)

UPD: Понял и осознал. Спасибо всем, кто ответил!

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
11 years ago, # |
  Vote: I like it +3 Vote: I do not like it
clock_t start = clock();

(clock() - start) / 1.0 / CLOCKS_PER_SEC;
  • »
    »
    11 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Почему бы вместо извратов просто не привести разницу к double?

    static_cast<double>(clock() - start) / CLOCKS_PER_SEC
    
    (double)(clock() - start) / CLOCKS_PER_SEC
    
    double(clock() - start) / CLOCKS_PER_SEC
    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      потому что делить на 1.0 удобнее, в свою очередь (потому что писать меньше символов и потому что обычно я вспоминаю что нужен каст только когда уже дописал разницу и не хочется возвращаться к началу строки:-) )

      Почти уверен, что генерируется абсолютно одинаковый код.

      • »
        »
        »
        »
        11 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        Почти уверен, что генерируется абсолютно одинаковый код.

        Any proof? It's not significant here but question is interesting itself.

        • »
          »
          »
          »
          »
          11 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Surely, I didn't check it when post the original message. Now I just tested it in my own laptop and binary files generate by g++ file

          #include <ctime>
          #include <iostream>
          using namespace std;
          
          int main(){
          	clock_t start = clock();
          	cout << double(clock() - start) / CLOCKS_PER_SEC << endl;
          //or 
          	cout << (clock() - start) / 1.0 / CLOCKS_PER_SEC << endl;
          	return 0;
          }
          

          are same. (diff sees no difference)

          Same for g++ -02 file

          • »
            »
            »
            »
            »
            »
            11 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            what difference between it on C++:

            (double) (a - b); and double(a - b);