Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance Dart:在多次调用的方法中编写嵌套函数是否有效 请考虑下面的代码, class A { foo() { int a = logicToGetA(); int _bar() => someOtherLogic(a); // .. int b = _bar(); // .. } } class B { int _bar(int a) => someOtherLogic(a); foo() { int a = logicToGetA(); // .. int b = _bar(a); // .. } } main() { for (int i = 0; i < 1000000; i++) { A().foo(); } for (int i = 0; i < 1000000; i++) { B().foo(); } } A类{ foo(){ int a=logicToGetA(); int_bar()=>someOtherLogic(a); // .. int b=_bar(); // .. } } B类{ int _bar(int a)=>someOtherLogic(a); foo(){ int a=logicToGetA(); // .. int b=_bar(a); // .. } } main(){ 对于(int i=0;i_Performance_Dart_Nested Function - Fatal编程技术网

Performance Dart:在多次调用的方法中编写嵌套函数是否有效 请考虑下面的代码, class A { foo() { int a = logicToGetA(); int _bar() => someOtherLogic(a); // .. int b = _bar(); // .. } } class B { int _bar(int a) => someOtherLogic(a); foo() { int a = logicToGetA(); // .. int b = _bar(a); // .. } } main() { for (int i = 0; i < 1000000; i++) { A().foo(); } for (int i = 0; i < 1000000; i++) { B().foo(); } } A类{ foo(){ int a=logicToGetA(); int_bar()=>someOtherLogic(a); // .. int b=_bar(); // .. } } B类{ int _bar(int a)=>someOtherLogic(a); foo(){ int a=logicToGetA(); // .. int b=_bar(a); // .. } } main(){ 对于(int i=0;i

Performance Dart:在多次调用的方法中编写嵌套函数是否有效 请考虑下面的代码, class A { foo() { int a = logicToGetA(); int _bar() => someOtherLogic(a); // .. int b = _bar(); // .. } } class B { int _bar(int a) => someOtherLogic(a); foo() { int a = logicToGetA(); // .. int b = _bar(a); // .. } } main() { for (int i = 0; i < 1000000; i++) { A().foo(); } for (int i = 0; i < 1000000; i++) { B().foo(); } } A类{ foo(){ int a=logicToGetA(); int_bar()=>someOtherLogic(a); // .. int b=_bar(); // .. } } B类{ int _bar(int a)=>someOtherLogic(a); foo(){ int a=logicToGetA(); // .. int b=_bar(a); // .. } } main(){ 对于(int i=0;i,performance,dart,nested-function,Performance,Dart,Nested Function,说明:类A的bar()嵌套在foo()内部,而类B的bar()嵌套在foo()外部。在第二种情况下,bar()也可以作为静态方法 我的疑问是:如果多次调用foo(),哪种方法更有效?如果A().foo()被调用1000000次,A.foo.bar会被重新定义那么多次吗?视情况而定 如果可以在foo方法之外定义\u bar函数,那么我们可以假定它不引用foo方法的任何局部变量。 在这种情况下,编译器可以优化局部函数,使其与实例方法一样高效。也许有,也许没有,让我们检查一下 请参阅:。 这是(试图)

说明:类A的bar()嵌套在foo()内部,而类B的bar()嵌套在foo()外部。在第二种情况下,bar()也可以作为静态方法

我的疑问是:如果多次调用foo(),哪种方法更有效?如果A().foo()被调用1000000次,A.foo.bar会被重新定义那么多次吗?

视情况而定

如果可以在
foo
方法之外定义
\u bar
函数,那么我们可以假定它不引用
foo
方法的任何局部变量。 在这种情况下,编译器可以优化局部函数,使其与实例方法一样高效。也许有,也许没有,让我们检查一下

请参阅:。 这是(试图)编写的,因此编译器无法优化调用
\u someOtherLogic
。 我还尝试将调用设为静态方法(但随后必须将对象本身作为参数传递,以便访问
标志的实例getter

在dartpad中运行此命令可以得到

A: 918460 /ms
B: 798960 /ms
S: 918380 /ms
使用局部函数似乎比使用实例方法更有效。 在VM上运行相同的代码会得到以下基准测试结果:

A: 625960.0 /ms
B: 723245.0 /ms
S: 625075.0 /ms
表明它的性能特征与dart2js完全相反

当静态已知时,dart2js很可能将
\u bar
函数内联。dart2js编译器在内联方面比VM更具攻击性

总而言之,我不会开始担心这种差异,除非函数调用在实际程序的性能配置文件中大量出现。
如果您的程序的性能确实严重依赖于这一个函数调用,我可能会内联函数。如果没有,写些可读性和可维护性更好的东西,在你知道这很重要之前不要开始微优化。

为什么这么多非母语人士说“我有疑问”,而他们的意思是“我有问题”?问题是中立的,怀疑意味着对情况或答案的某种怀疑。只是好奇。“一个疑问”在印度英语中对于大多数其他英语语言中被称为“一个问题”是完全正确的。()我认为“怀疑”是一种谦虚的提问方式,即“我明白你的意思,但我需要澄清一下”。然而,一个“问题”可以少一点尊重。就像上面给出的审问例子,我向你的专业知识致敬。非母语为印度英语的人很容易忽略这些细微差别。我自学了如何将“怀疑”理解为“问题”,这对我在国际公司工作到目前为止都很有用,可能是因为“问题”这个词在美英英语中是中性的(至少对我来说)。@lrn你的方法更合理。我应该避免使用“怀疑”作为可数名词。