append_zero 浮点和double补充0 库函数PK自己写的函数效率对比
2015-11-25 17:44
393 查看
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();
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- PowerShell函数简明教程
- 探索PowerShell(十一)函数介绍