append_zero 浮点和double补充0 库函数PK自己写的函数效率对比

来源:互联网 发布:大数据 医学的统计 编辑:程序博客网 时间:2024/06/04 19:16

define USING_LOG_PREFIX RPC_TEST

include

include “rpc/obmysql/ob_mysql_util.h”

using namespace oceanbase::common;
using namespace oceanbase::obmysql;

class TestDoubleZeroFill
: public ::testing::Test
{
public:
TestDoubleZeroFill()
{}

virtual void SetUp()
{
}

virtual void TearDown()
{
}

void zero_fill_libraries(char *buf, int64_t &length, int16_t scale)
{
if (scale > 0) {
int point = 0;
const char *findponit = buf;
const char *pot = strchr(findponit,’.’);
if (pot !=NULL)
{
point = static_cast(pot - findponit);
} else {
point = static_cast(length);
}
if (point == length) {
// *(buf + length) = ‘.’;
memcpy(buf+ length, “.”, 1);
length ++;
}
memset(buf + length, ‘0’, scale + 1 - length + point);
length += scale + 1 - length - point;
*(buf + length) = ‘\0’;
}
}

void ObMySQLUtil::zero_fill(char *buf, int64_t &length, int16_t scale)
{
if (scale > 0) {
int point = 0;
for (;*(buf + point) != ‘.’ && point < length; point ++) { }
if (point == length) {
*(buf + length) = ‘.’;
length ++;
}
for (int64_t i = length-point; (scale - i + 1) >0; i++) {
*(buf + length) = ‘0’;
length ++;
}
*(buf + length) = ‘\0’;
}
}

protected:
};

TEST_F(TestDoubleZeroFill, TestFillZero)
{
const char org = (const char )”623423.234”;
char buf[64];
int64_t char_size = strlen(org);
int16_t scale = 10;

    const char *org_1 = (const char *)"3.2";    int64_t char_size_1 = strlen(org_1);    int16_t scale_1 = 2;    int64_t total_my = 0;    int64_t total_lib = 0; for (int j = 0; j<5; j++) {    memset(buf, 0, 64);    memcpy(buf, org, char_size);    const int64_t start_ts_my = ObTimeUtility::current_time();    for (int i = 0; i < 100000000; i++)    {            zero_fill(buf, char_size, scale);    }    const int64_t end_ts_my = ObTimeUtility::current_time();    memset(buf, 0, 64);    memcpy(buf, org, char_size);    const int64_t start_ts_lib = ObTimeUtility::current_time();    for (int i = 0; i < 100000000; i++)    {            zero_fill_libraries(buf, char_size, scale);    }    const int64_t end_ts_lib = ObTimeUtility::current_time();    total_my += end_ts_my - start_ts_my;    total_lib += end_ts_lib - start_ts_lib;    memset(buf, 0, 64);    memcpy(buf, org_1, char_size_1);    const int64_t start_ts_my_1 = ObTimeUtility::current_time();    for (int i = 0; i < 100000000; i++)    {       zero_fill(buf, char_size_1, scale_1);    }   const int64_t end_ts_my_1 = ObTimeUtility::current_time();    memset(buf, 0, 64);    memcpy(buf, org_1, char_size_1);    const int64_t  start_ts_lib_1 = ObTimeUtility::current_time();    for (int i = 0; i < 100000000; i++)    {            zero_fill_libraries(buf, char_size_1, scale_1);    }    const int64_t end_ts_lib_1 = ObTimeUtility::current_time();    total_my += end_ts_my_1 - start_ts_my_1;    total_lib += end_ts_lib_1 - start_ts_lib_1; } LOG_INFO("cmp, ob vs lib", K(total_my), K(total_lib));

}

int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

0 0
原创粉丝点击