本文介绍了sprintf的VS的String.Format性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较sprintf的用法的性能,是由什么我看见一个小困扰。我测试了以下4种方法,通过ClassWithToString的一个实例中的每个(除了PrintInt,并获得实际的整数值)。

 键入ClassWithToString()=
    会员this.X = 42
    覆盖this.ToString()= this.X.ToString()

让打印的项目:字符串=
    sprintf的%A项

让PrintInt项目:字符串=
    sprintf的%i的项目

让PrintObj项目:字符串=
    sprintf的%O项目

让格式项:字符串=
    System.String.Format({0}项)
 

结果50000迭代:

 打印(%A):3143ms
PrintInt(%I):355ms
PrintObj(%O):384ms
格式:8毫秒
 

有关打印,我理解%A被使用反射使呆滞没有令人震惊,尽管对于50K迭代我很惊讶的总时间。以下的是,PrintInt和PrintObj不使用反射,因此被一个数量级更快,这也很有意义。

这是混淆了我的部分是,在光的String.Format的结果()的的sprintf一般似乎是不堪慢(并已见证了配置文件现场应用程序)。为什么比的String.Format慢sprintf的大小()?有没有在F#的空间更好的选择,我已经错过了吗?

解决方案

这不是真的。所有的printf 函数需要反思,从格式字符串来构造类型安全打印功能,无论哪个说明符使用。看看此行和的 的printf 中更多的内幕模块。所以很容易看出为什么 sprintf的%I仍比的String.Format 慢。在的情况下,sprintf的%A,它有反射多了一个水平,这说明它的可怕缓慢。

如果你的目的是构建大串,的StringWriter 和 StringBuilder的可能是长的路要走。如果基准记录的目的, FastPrintf 是一种很有前途的图书馆。您可以尝试此的NuGet包号称比内置的的printf 功能。

I was comparing the performance of sprintf usages and am a little troubled by what I saw. I tested the following 4 methods, passing an instance of ClassWithToString in to each (except for PrintInt, which received the actual integer value).

type ClassWithToString() =
    member this.X = 42
    override this.ToString() = this.X.ToString()

let Print item : string =
    sprintf "%A" item

let PrintInt item: string =
    sprintf "%i" item

let PrintObj item: string =
    sprintf "%O" item

let Format item : string =
    System.String.Format("{0}", item)

The results for 50,000 iterations:

Print (%A):     3143ms
PrintInt (%i):   355ms
PrintObj (%O):   384ms
Format:            8ms

For "Print," I understand %A is using reflection so the sluggishness there is not shocking, though for 50k iterations I was surprised at the total time. Following that, "PrintInt" and "PrintObj" do not use reflection and thus are an order of magnitude faster, which also makes sense.

The part that confounds me is that in light of the results of String.Format() sprintf in general appears to be dreadfully slow (and has been witnessed in profiles of live applications). Why is sprintf magnitudes slower than String.Format()? Is there a better alternative in the F# space that I've missed?

解决方案

It's not true. All printf functions need reflection to construct type-safe printing functions from format strings, no matter which specifiers you use. Take a look at this line and this line from printf module for more insights. So it's easy to see why sprintf "%i" is still slower than String.Format.In the case of sprintf "%A", it has one more level of reflection which explains its dreadful slowness.

If your purpose is to construct big strings, StringWriter and StringBuilder are probably the ways to go. If you benchmark for logging purpose, FastPrintf is a promising library. You can try out this NuGet package which claims to be 100x faster than the built-in printf functions.

这篇关于sprintf的VS的String.Format性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:34