博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
double在输出为字符串的几种方法效率测试
阅读量:4537 次
发布时间:2019-06-08

本文共 2696 字,大约阅读时间需要 8 分钟。

测试结果:

 

double->none 366ms
double->long 161ms
double->long2 188ms
double->format 564ms
double->Round 393ms

 

代码:

 

using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace testStringbuilder{    class Program    {        static void Main(string[] args)        {            const int count = 1000000;            RunTest("double->none",()=>            {                StringBuilder sb = new StringBuilder(1000*1024*32);                double a = 3.1415926;                for (int i = 0; i < count; i++)                {                    sb.Append(a);                    sb.Append(',');                }            });            RunTest("double->long", () =>            {                StringBuilder sb = new StringBuilder(1000 * 1024 * 32);                double a = 3.1415926;                for (int i = 0; i < count; i++)                {                    sb.Append((long)a);                    sb.Append(',');                }            });            RunTest("double->long2", () =>            {                StringBuilder sb = new StringBuilder(1000 * 1024 * 32);                double a = 3.1415926;                for (int i = 0; i < count; i++)                {                    sb.Append((long)(a * 100));                    sb.Append(',');                }            });            RunTest("double->format", () =>            {                StringBuilder sb = new StringBuilder(1000 * 1024 * 32);                double a = 3.1415926;                for (int i = 0; i < count; i++)                {                    sb.AppendFormat("{0:f3}",a);                    sb.Append(',');                }            });            RunTest("double->Round", () =>            {                StringBuilder sb = new StringBuilder(1000 * 1024 * 32);                double a = 3.1415926;                for (int i = 0; i < count; i++)                {                    sb.Append(Math.Round(a, 3));                    sb.Append(',');                }            });        }        private static void RunTest(string key, Action action)        {            double milli = 0;            for (int i = 0; i < 3; i++)            {                Stopwatch watch = Stopwatch.StartNew();                try                {                    action();                }                catch (Exception ex)                {                    Console.WriteLine(ex);                }                watch.Stop();                milli += watch.ElapsedMilliseconds;            }            Console.WriteLine("{0}\t{1}ms", key, (long) (milli/3));        }    }}

  

转载于:https://www.cnblogs.com/yahle/p/3476826.html

你可能感兴趣的文章
DELPHI 调用系统 ADO 配置窗体 提高软件易用性
查看>>
Mongodb 命令及 PyMongo 库的使用
查看>>
div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(非原创)
查看>>
关于SDWebImage加载高清图片导致app崩溃的问题
查看>>
如何查看方法在哪里被调用
查看>>
HUE的自动化安装部署
查看>>
图片服务器(FastDFS)的搭建
查看>>
myBatis应用
查看>>
RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
查看>>
[PHP] 用AppServ一步到位安装PHP服务器
查看>>
mac brew install redis 报错
查看>>
Work? working!
查看>>
给UINavigationBar自定义颜色
查看>>
开源收藏
查看>>
CentOS7下FTP的安装与配置
查看>>
即使是学习,也还是多出门学习比较好!
查看>>
zoj 3432 Find the Lost Sock (ZOJ Monthly, November 2010)
查看>>
linux目录结构
查看>>
数据库索引分类
查看>>
python 内置函数
查看>>