LoginSignup
1
1

More than 5 years have passed since last update.

C++ Builder XE4 > difference > try,catchでStrToFloat()を囲うよりは、TryStrToFloat()を使う方が速い

Last updated at Posted at 2018-11-23
動作環境
C++ Builder XE4

調査内容概要

  • StrToFloat()で数値でない文字列を扱う場合がある
  • エラー処理のためtry, catchを使った
  • 遅い
  • 別途、TryStrToFloat()を見つけたので使った
  • 速い

処理時間の違いについて実測した。

関連

実装

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <VCLTee.Chart.hpp>
#include <VCLTee.Series.hpp>
#include <VCLTee.TeEngine.hpp>
#include <VCLTee.TeeProcs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TChart *Chart1;
    TFastLineSeries *Series1;
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <DateUtils.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Chart1->Series[0]->XValues->DateTime = true;
    Chart1->BottomAxis->DateTimeFormat = L"nn:ss";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    DWORD stTim, edTim; // 処理時間計測用

    TDateTime loopdt;

    loopdt = Now();

    String datstr;

    static const int kNumLoop = 100;
    double yval;

    // 1. try, catch ==================================================
    datstr = L"Y";
    stTim = GetTickCount();  // 処理時間計測用
    for (int idx=0; idx < kNumLoop; idx++) {
        if (idx > kNumLoop/2) { // 途中から値にする
            datstr = "3.14";
        }

        try {
            yval = StrToFloat(datstr);
        } catch (...) {
            loopdt = IncSecond(loopdt, 1);
            continue;
        }
        Series1->AddXY(loopdt, yval, "", clRed);
        loopdt = IncSecond(loopdt, 1);
    }

    edTim = GetTickCount() - stTim;  // 処理時間計測用
    OutputDebugString(IntToStr((int)edTim).c_str());  // 処理時間計測用


    // 2. TryStrToFloat() ==================================================
    datstr = L"Y";
    stTim = GetTickCount();  // 処理時間計測用
    for (int idx=0; idx < kNumLoop; idx++) {
        if (idx > kNumLoop/2) { // 途中から値にする
            datstr = "3.14";
        }

        if (TryStrToFloat(datstr, yval) == false) {
            loopdt = IncSecond(loopdt, 1);
            continue;
        }
        Series1->AddXY(loopdt, yval, "", clRed);
        loopdt = IncSecond(loopdt, 1);
    }

    edTim = GetTickCount() - stTim;  // 処理時間計測用
    OutputDebugString(IntToStr((int)edTim).c_str());  // 処理時間計測用



}
//---------------------------------------------------------------------------

結果

デバッグ出力: 141 プロセス Project1.exe (5164)
デバッグ出力: 0 プロセス Project1.exe (5164)

2の場合は0 (msec)。
1msec以下の処理ということなのだろう。

試しに順番を入れ替えた場合は下記。やはりTryStrToFloat()の方が速い。

デバッグ出力: 0 プロセス Project1.exe (1784)
デバッグ出力: 125 プロセス Project1.exe (1784)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1