Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.
動作環境
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)
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away