From e49f5fbb3376d980d31b92cb83a8855526e8725e Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Thu, 17 Oct 2019 16:26:34 +0200 Subject: [PATCH] TT#69200 Use sprintf instead of ad-hoc code to 0 fill digits Which is also around 300% faster: Rate had-hoc sprintf had-hoc 3508772/s -- -75% sprintf 14084507/s 301% -- Change-Id: I5ebce0058473f9de08c381739646a20e42308a8f (cherry picked from commit 561e36d08c31f215896d9d949484c31942a235ee) --- debian/control | 7 +++++++ lib/NGCP/BulkProcessor/Utils.pm | 10 ++-------- t/Utils.t | 30 ++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 t/Utils.t diff --git a/debian/control b/debian/control index f7e5980..ea529c4 100644 --- a/debian/control +++ b/debian/control @@ -4,7 +4,14 @@ Priority: optional Maintainer: Sipwise Development Team Build-Depends: debhelper-compat (= 12), + libdata-uuid-perl , + libdata-validate-ip-perl , + libdate-calc-perl , + libdate-manip-perl , libmodule-build-perl, + libnet-address-ip-local-perl , + libsys-cpuaffinity-perl , + libuuid-perl , Standards-Version: 3.9.8 Homepage: https://www.sipwise.com/ diff --git a/lib/NGCP/BulkProcessor/Utils.pm b/lib/NGCP/BulkProcessor/Utils.pm index c7877c5..c63b823 100644 --- a/lib/NGCP/BulkProcessor/Utils.pm +++ b/lib/NGCP/BulkProcessor/Utils.pm @@ -440,14 +440,8 @@ sub get_year_month_day { sub zerofill { my ($integer,$digits) = @_; - my $numberofzeroes = $digits - length($integer); - my $resultstring = $integer; - if ($digits > 0) { - for (my $i = 0; $i < $numberofzeroes; $i += 1) { - $resultstring = "0" . $resultstring; - } - } - return $resultstring; + + return sprintf '%0*d', $digits, $integer; } sub trim { diff --git a/t/Utils.t b/t/Utils.t new file mode 100644 index 0000000..d184942 --- /dev/null +++ b/t/Utils.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use strict; +use warnings; + +use Test::More tests => 4; + +require_ok('NGCP::BulkProcessor::Utils'); + +NGCP::BulkProcessor::Utils->import(qw( + zerofill +)); + +# zerofill() +is(zerofill(0, 4), '0000'); +is(zerofill(25, 4), '0025'); +is(zerofill(1000, 4), '1000');