[UDF] Improve performance of function money_format (#4672)
Use static local variable instead of create it every calls. Time cost of the new added unit benchmark test could reduce from about 60 seconds to 10 seconds.
This commit is contained in:
@ -181,9 +181,16 @@ public:
|
||||
};
|
||||
|
||||
static StringVal do_money_format(FunctionContext *context, const std::string& v) {
|
||||
std::locale comma_locale(std::locale(), new CommaMoneypunct ());
|
||||
std::stringstream ss;
|
||||
ss.imbue(comma_locale);
|
||||
static std::locale comma_locale(std::locale(), new CommaMoneypunct());
|
||||
static std::stringstream ss;
|
||||
static bool ss_init = false;
|
||||
if (UNLIKELY(!ss_init)) {
|
||||
ss.imbue(comma_locale);
|
||||
ss_init = true;
|
||||
}
|
||||
static std::string empty_string;
|
||||
ss.str(empty_string);
|
||||
|
||||
ss << std::put_money(v);
|
||||
return AnyValUtil::from_string_temp(context, ss.str());
|
||||
};
|
||||
|
||||
@ -41,6 +41,16 @@ private:
|
||||
FunctionContext* ctx;
|
||||
};
|
||||
|
||||
TEST_F(StringFunctionsTest, do_money_format_bench) {
|
||||
doris_udf::FunctionContext* context = new doris_udf::FunctionContext();
|
||||
StringVal expected = AnyValUtil::from_string_temp(context, std::string("9,223,372,036,854,775,807.00"));
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
StringVal result = StringFunctions::do_money_format(context, "922337203685477580700"); // cent
|
||||
ASSERT_EQ(expected, result);
|
||||
}
|
||||
delete context;
|
||||
}
|
||||
|
||||
TEST_F(StringFunctionsTest, money_format_bigint) {
|
||||
doris_udf::FunctionContext* context = new doris_udf::FunctionContext();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user