简体   繁体   English

Obj-C:类方法与实例方法 - 效率差异?

[英]Obj-C: Class methods vs instance methods — efficiency difference?

For the sake of tidiness and reusability, I'm thinking about putting a bunch of utility methods inside a single class. 为了整洁和可重用性,我正在考虑在一个类中放入一堆实用程序方法。 The class does not require any properties of its own and so I will define the methods as class methods; 该类不需要任何自己的属性,因此我将方法定义为类方法; I figure that's there's no real need to have an instance of this class floating around… 我认为没有真正需要让这个类的实例浮动...

…or is there? ......还是在那儿? Is there a difference in efficiency? 效率有差异吗? (A noticeable one?) Or does calling class methods behave in the same way as if they were part of the calling class? (一个引人注目的?)或者调用类方法的行为方式与调用类的一部分相同吗?

dandan78 has the correct answer in general. dandan78一般有正确的答案。 It's always best to measure your code yourself. 最好自己测量代码。 In fact, for this problem, you should probably just implement it the way you want, and then measure, and only change it if it's significantly slowing down your app. 事实上,对于这个问题,你应该只是按照你想要的方式实现它,然后测量,只有当它显着减慢你的应用程序时才改变它。

Gavin is also correct that C functions are more efficient than class or instance methods. Gavin也是正确的,C函数比类或实例方法更有效。 But again, unless you're calling these methods millions of times in a tight loop, the overhead of a method vs a C function probably won't matter to your application. 但同样,除非您在紧密循环中调用这些方法数百万次,否则方法与C函数的开销可能对您的应用程序无关紧要。

But to answer the question asked, there's basically no difference between class methods and instance methods. 但是为了回答问题,类方法和实例方法之间基本没有区别。 In Objective-C, classes are actually objects themselves, and sending messages to them is identical to sending a message to an instance object. 在Objective-C中,类实际上是对象本身,向它们发送消息与向实例对象发送消息完全相同。 Each class basically has a singleton object that class methods are called on. 每个类基本上都有一个单例对象,可以调用类方法。

As in all cases regarding efficiency-related dilemmas, your best bet is to do what makes sense from a coding point of view -- make the code as intuitive and reader-friendly as you can. 在所有关于与效率相关的困境的情况下,最好的办法是从编码的角度做有意义的事情 - 让代码尽可能直观且易于阅读。 If it turns out that something is not as fast as it needs to be, profile and optimize accordingly. 如果事实证明事情并不像它需要的那么快,那么相应地进行剖析和优化。

And although I don't expect there to be any difference in this case, you can make a class and a instance version of a method, run it a million times in a loop preceded and followed by [NSDate date] calls, then compare the times. 虽然我不希望在这种情况下有任何不同,你可以创建一个方法的类和实例版本,在一个循环中运行它一百万次,然后是[NSDate date]调用,然后比较倍。

The most efficient way would be to make them C functions, actually. 实际上,最有效的方法是使它们成为C函数。 You'll avoid the overhead of the Objective-C runtime every time you call one of them. 每次调用其中一个时,您都将避免Objective-C运行时的开销。 Obviously based on the wording of your question, you don't need extra state that you keep around for these utility methods, since you were thinking about making them class methods anyway. 显然,基于你的问题的措辞,你不需要为这些实用方法保留额外的状态,因为你无论如何都在考虑使它们成为类方法。

When I keep around utility methods like this, I most of the time make them C functions, because it's cleanest and most efficient. 当我保持这样的实用方法时,我大部分时间都将它们作为C函数,因为它是最干净,最有效的。 When I might make an Objective-C class with them as methods is if they have something in common with each other where it makes sense to have them organized this way, or if I need to keep state around for them. 当我可以用它们制作一个Objective-C类作为方法时,如果它们彼此有共同点,那么以这种方式组织它们是有意义的,或者如果我需要为它们保持状态。

I made a sample project that compares calling instance methods to class methods or a C function. 我做了一个示例项目 ,将调用实例方法与类方法或C函数进行比较。 Here's some example output: 这是一些示例输出:

Class methods:    Time to complete: 0.00415796
Single instance:  Time to complete: 0.00437099
Using Singleton:  Time to complete: 0.071667
Using Global:     Time to complete: 0.00534797
Using C function: Time to complete: 0.00302202

The primary conclusion you should get from this is that the differences are negligible ; 你应该得出的主要结论是差异可以忽略不计 ; use Instruments to profile for actual performance bottlenecks. 使用Instruments来分析实际的性能瓶颈。

To get back to your original question, you should use a class method, since your methods don't depend on any state (properties) that an instance might hold. 要回到原始问题,您应该使用类方法,因为您的方法不依赖于实例可能包含的任何状态(属性)。 Performance wise, this can only help because using an actual instance means you have to allocate and access that instance somehow, which adds overhead (again, all this is negligible). 性能方面,这只会有所帮助,因为使用实际实例意味着您必须以某种方式分配和访问该实例,这会增加开销(同样,这一切都可以忽略不计)。

Using a C function will be unnoticeably quicker, but you won't be able to subclass and override that C function like you would a class method. 使用C函数会更快,但是你不能像类方法那样子类化和覆盖那个C函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM