Patchwork [7/8] cpu: cpufreq: make HzToHuman() more efficient

login
register
mail settings
Submitter Colin King
Date July 23, 2012, 1:02 p.m.
Message ID <1343048578-13332-8-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/172666/
State Accepted
Headers show

Comments

Colin King - July 23, 2012, 1:02 p.m.
From: Colin Ian King <colin.king@canonical.com>

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/cpu/cpufreq/cpufreq.c |   22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
Keng-Yu Lin - July 25, 2012, 7:27 a.m.
On Mon, Jul 23, 2012 at 9:02 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/cpu/cpufreq/cpufreq.c |   22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index f4a461b..8cd4da0 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -216,23 +216,23 @@ static unsigned long get_performance_repeat(fwts_framework *fw,
>
>  static char *HzToHuman(unsigned long hz)
>  {
> -       static char buffer[1024];
> +       static char buffer[32];
>         unsigned long long Hz;
>
>         Hz = hz;
>
> -       /* default: just put the Number in */
> -       snprintf(buffer, sizeof(buffer), "%9lli", Hz);
> -
> -       if (Hz > 1000)
> -               snprintf(buffer, sizeof(buffer), "%6lli Mhz",
> -                       (Hz+500) / 1000);
> -
> -       if (Hz > 1500000)
> +       if (Hz > 1500000) {
>                 snprintf(buffer, sizeof(buffer), "%6.2f Ghz",
>                         (Hz+50000.0) / 1000000);
> -
> -       return buffer;
> +               return buffer;
> +       } else if (Hz > 1000) {
> +               snprintf(buffer, sizeof(buffer), "%6lli Mhz",
> +                       (Hz+500) / 1000);
> +               return buffer;
> +       } else {
> +               snprintf(buffer, sizeof(buffer), "%9lli", Hz);
> +               return buffer;
> +       }
>  }
>
>
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu - July 25, 2012, 8:03 a.m.
On 07/23/2012 09:02 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/cpu/cpufreq/cpufreq.c |   22 +++++++++++-----------
>   1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index f4a461b..8cd4da0 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -216,23 +216,23 @@ static unsigned long get_performance_repeat(fwts_framework *fw,
>
>   static char *HzToHuman(unsigned long hz)
>   {
> -	static char buffer[1024];
> +	static char buffer[32];
>   	unsigned long long Hz;
>
>   	Hz = hz;
>
> -	/* default: just put the Number in */
> -	snprintf(buffer, sizeof(buffer), "%9lli", Hz);
> -
> -	if (Hz > 1000)
> -		snprintf(buffer, sizeof(buffer), "%6lli Mhz",
> -			(Hz+500) / 1000);
> -
> -	if (Hz > 1500000)
> +	if (Hz > 1500000) {
>   		snprintf(buffer, sizeof(buffer), "%6.2f Ghz",
>   			(Hz+50000.0) / 1000000);
> -
> -	return buffer;
> +		return buffer;
> +	} else if (Hz > 1000) {
> +		snprintf(buffer, sizeof(buffer), "%6lli Mhz",
> +			(Hz+500) / 1000);
> +		return buffer;
> +	} else {
> +		snprintf(buffer, sizeof(buffer), "%9lli", Hz);
> +		return buffer;
> +	}
>   }
>
>
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
index f4a461b..8cd4da0 100644
--- a/src/cpu/cpufreq/cpufreq.c
+++ b/src/cpu/cpufreq/cpufreq.c
@@ -216,23 +216,23 @@  static unsigned long get_performance_repeat(fwts_framework *fw,
 
 static char *HzToHuman(unsigned long hz)
 {
-	static char buffer[1024];
+	static char buffer[32];
 	unsigned long long Hz;
 
 	Hz = hz;
 
-	/* default: just put the Number in */
-	snprintf(buffer, sizeof(buffer), "%9lli", Hz);
-
-	if (Hz > 1000)
-		snprintf(buffer, sizeof(buffer), "%6lli Mhz",
-			(Hz+500) / 1000);
-
-	if (Hz > 1500000)
+	if (Hz > 1500000) {
 		snprintf(buffer, sizeof(buffer), "%6.2f Ghz",
 			(Hz+50000.0) / 1000000);
-
-	return buffer;
+		return buffer;
+	} else if (Hz > 1000) {
+		snprintf(buffer, sizeof(buffer), "%6lli Mhz",
+			(Hz+500) / 1000);
+		return buffer;
+	} else {
+		snprintf(buffer, sizeof(buffer), "%9lli", Hz);
+		return buffer;
+	}
 }