Will the iPhone app use performance if I use Objective-C for low level code?

When programming heavy or heavy CPU usage on an iPhone or other portable equipment, you need to make the right algorithmic decisions to quickly make your code.

But even a great choice of algorithm can be slow if the language you use works worse than the other.

Is there any hard data comparing Objective-C with C ++, especially on the iPhone, but perhaps only on the Mac desktop, to perform various similar aspects of the language? I am well acquainted with this article comparing C and Objective-C , but this is a more important question about comparing two object-oriented languages ​​with each other.

For example, is a C ++ vtable search really faster than an Obj-C message? How much faster? Threading, polymorphism, sorting, etc. Before moving on to the task of creating a project with repeating object models and various test code, I want to know someone has already done this and what the results are. This type of testing and comparison is a project in itself and can take a considerable amount of time. Perhaps this is not one project, but two and only outputs can be compared.

I am looking for hard data , not evangelism. Like many of you, I love and hate both languages ​​for various reasons. In addition, if someone there is actively pursuing the same thing, it will be interesting for me to download in some code to see the final results, and I am sure that others will also help. I assume that both of them have strengths and weaknesses, my goal is to find out exactly what they are doing so that they can be avoided / used in real life scenarios.

+68
c ++ performance optimization objective-c iphone
May 29 '09 at 15:53
source share
8 answers

Mike Ash has some hard numbers for making various calls to the Objective-C method versus C and C ++ in his post Comparison of the Performance of General Operations . Also, this Savoy Software post is interesting when it comes to tuning the performance of an iPhone application using Objective-C ++.

I prefer the clean, descriptive syntax of Objective-C over Objective-C ++, and I have not found the language itself as the source of my performance bottlenecks. I even tend to do what I know, sacrificing a bit of performance if they make my code much more convenient.

+57
May 30 '09 at 12:45 a.m.
source share

Yes, well-written C ++ is much faster. If you write performance-critical programs, and your C ++ is not as fast as C (or a few percent), something is wrong. If your ObjC implementation runs as fast as C, then something is usually wrong - that is, the program is probably a bad example of ObjC OOD because it probably uses some dirty tricks to step below the level the abstraction in which he works, for example, direct ivar.

Comparing Mike Ashe "is very misleading - I would never recommend an approach to comparing the runtime of the programs you wrote, or recommend comparing C vs C ++ with ObjC. The presented results are provided from a test with disabling compiler optimization. A program compiled with disabled optimizations are rarely relevant when you measure the runtime.To view it as a reference, which compares C ++ with Objective-C, is a drawback.The test also compares individual functions, not whole ones realistic implementations - individual functions are combined in different ways with both languages. This is far from a realistic performance test for optimized implementations. Examples. When optimization is enabled, the IMP cache is as slow as calling virtual functions. Static dispatch (unlike dynamic sendings, for example, using virtual ) and calls to known C ++ types (where dynamic sending can be bypassed) can be optimized aggressively. This process is called devirtualization, and when used, the member function declared virtual may even be inline d. In the case of the Mike Ash test, where many calls are made with member functions that have been declared virtual and have empty bodies: these calls are fully optimized when the type is known, because the compiler sees the implementation and can determine if a dynamic dispatch is needed. The compiler can also eliminate malloc calls in optimized builds (in favor of stack storage). Thus, the inclusion of compiler optimizers in any of C, C ++, or Objective-C can lead to significant differences in runtime.

Not to say that the results presented are absolutely useless. You can get useful information about external APIs if you want to determine if there are measurable differences between the time they spend in pthread_create or +[NSObject alloc] on one platform or architecture compared to another. Of course, these two examples will use optimized implementations in your test (unless you are developing them). But to compare one language with another in the programs you compile ... the presented results are useless when optimizations are disabled.

Object Creation

Consider also creating an object in ObjC - each object is allocated dynamically (for example, on the heap). With C ++, objects can be created on the stack (for example, about as fast as creating a C structure and calling a simple function in many cases), on the heap, or as elements of abstract data types. Each time you allocate and release (for example, via malloc / free), you can enter a lock. When you create a C struct or C ++ object on the stack, locking is not required (although internal members can use heap allocation), and it often costs just a few instructions or a few instructions plus a function call.

In addition, ObjC objects are reference counted instances. The actual need for an object to be std::shared_ptr in critical C ++ performance is very rare. In C ++, it is not necessary or desirable to make each instance a shared instance with reference counting. You have much more control over ownership and lifetime with C ++.

Arrays and Collections

Arrays and many collections in C and C ++ also use strongly typed containers and continuous memory. Since the address of the next element members is often known, the optimizer can do much more, and you have a lot of cache and memory. With ObjC, which is far from reality for standard objects (for example, NSObject ).

Dispatch

Regarding methods, many C ++ implementations use several virtual / dynamic calls, especially in highly optimized programs. These are static method calls and optimizer feeds.

Using ObjC methods, each method call (sending an objc message) is dynamic and therefore is a firewall for the optimizer. Ultimately, this leads to many limitations or inconveniences regarding what you can and cannot do to maintain performance at least when writing critical ObjC. This can lead to larger methods, IMP caching, frequent use of C.

Some real-time applications cannot use any ObjC messaging in their visualization paths. No - sound rendering is a good example of this. Submitting ObjC is simply not intended for real-time purposes; Highlighting and blocking can occur behind the scenes during messaging, which makes objc's complexity / messaging time unpredictable so that audio and video can miss its deadline.

Other functions

C ++ also provides generic / template implementations for many of its libraries. They are very well optimized. They are typical, and many investments and optimizations can be done using templates (consider its polymorphism, optimization and specialization that occur during compilation). C ++ adds several functions that are simply not available or comparable in strict ObjC. Trying to directly compare langs, objects and libraries, which are very different, is not so useful - this is a very small subset of real implementations. It is better to expand the question to a library / framework or a real program, taking into account many aspects of design and implementation.

Other points

Symbols C and C ++ can be easily removed and optimized at different stages of assembly (deletion, deletion of dead code, embedding and early insertion, as well as optimization of communication time). Benefits of this include reduced binary sizes, reduced startup / boot time, reduced memory consumption, etc. For one application, this may not be so important; but if you reuse a lot of code, and you should, then your shared libraries could add a lot of extra weight to the program, if implemented by ObjC - if you are not ready to jump over some flaming hoops. Thus, scalability and reuse are also factors in medium and large projects and groups where it is reused.

Included Libraries

ObjC library developers are also optimized for the environment, so its library developers can use some of the language and environment functions to offer optimized implementations. Despite some fairly significant limitations when writing an optimized program in pure ObjC, some highly optimized implementations exist in Cocoa. This is one of Cocoa's strengths, although the standard C ++ library (what some call STL) doesn't slouch either. Cocoa works at a much higher level of abstraction than C ++ - if you have a poor knowledge of what you are doing (or should do), working closer to the metal can really cost you. A return to a good library implementation if you are not an expert in some domain is good if you are really not ready to learn. In addition, Cocoa environments are limited; you can find implementations / optimizations that make better use of the OS.

If you write optimized programs and gain experience in C ++ and ObjC, pure C ++ implementations will often be twice as fast or faster than pure ObjC (yes, you can compare with Cocoa). If you know how to optimize, you can often do better than general abstractions. Although some optimized C ++ implementations will be as fast or slow as Cocoa (for example, my initial attempt to file I / O was slower than Cocoa - primarily because the C ++ implementation initializes its memory).

Many of them come down to language features that you are familiar with. I use both languages, they both have different strengths and models / patterns. They complement each other well, and there are large libraries for them. If you are running a complex performance-critical program, using the C ++ functions and libraries correctly will give you much more control and significant optimization benefits, so that in the right hands “several times faster” is a good default expectation (however, don't expect winning every time, or without any work). Remember that years are enough to understand C ++ to really reach this point.

I keep most of the critical working paths as C ++, but also acknowledge that ObjC is also a very good solution for some problems and that there are some very good libraries.

+54
Apr 10 2018-12-21T00:
source share

It is very difficult to collect “hard data” for this, so as not to be misleading.

The biggest problem with comparing functions with functions, you think, is that the two languages ​​support very different coding styles. Objective-C is a dynamic duck typing language where the typical use of C ++ is static. The same problem with an object-oriented architecture is likely to have very different ideal solutions using C ++ or Objective-C.

My feeling (since I programmed a lot in both languages, mainly on huge projects): To maximize the performance of Objective-C, you need to write it very close to C. While with C ++, you can do a lot more using the language without any Any performance limitations compared to C.

Which one is better? I dont know. For pure performance, C ++ will always have an advantage. But the OOP Objective-C style definitely has its merits. I definitely think it’s easier to maintain a reasonable architecture.

+31
May 29 '09 at 16:11
source share

Actually, this is not something that can be answered in general, since it really depends on how you use the language functions. In both languages ​​there will be things in which they are fast, at which they are slow, and also on things that are sometimes fast and sometimes slow. It really depends on what you use and how you use it. The only way to be sure is to profile your code.

You can also write C ++ code in Objective-C, so it may be easier to compose code in Objective-C, and if you find something that doesn't work in it, then you can ask to write a C ++ version and vision, it helps whether it is (C ++ seeks to optimize compilation time). Objective-C will be easier to use if the APIs you interact with are written in it, plus you can find its OOP style easier or more flexible.

In the end, you have to go so that, as you know, you can write safe, reliable code, and if you find an area that needs special attention from another language, then you can change it. X-Code allows you to compile both in one project.

+7
Nov 25 '10 at 13:44
source share

I don't have hard data for Objective-C, but I have a good place to search for C ++.

C ++ started as C with classes according to the Bjarne Stroustroup in its reflection in the early years of C ++ ( http://www2.research.att.com/~bs/hopl2.pdf ), so C ++ can be considered ( for example, Objective C) as pushing C to its limits to orient the object.

What are these limitations? In 1994-1997, many researchers found that object orientation is caused by cost due to dynamic binding, for example. when C ++ functions are marked as virtual, and may / may not be child classes that override these functions. (In Java and C #, all functions expect ctors to be virtual in nature, and there is not much you can do about it.) In Learning Destructuring Techniques for Java Just-In-Time Compiler from IBM Research Tokyo, they compare methods used to combat this, including those from Urz Hölsle and Gerald Aigner. Urz Hölsle in a separate article with Karel Driesen showed that on average 5.7% of the time in C ++ programs (and up to ~ 50%) was spent when calling virtual functions (for example, vtables + thunks). He later worked with some Smalltalk researchers to end the Java HotSpot VM to solve these problems in OO. Some of these functions access C ++ (for example, "protected" and exception handling).

As I mentioned, C ++ is statically typed, where Objective-C is typed duck. The difference in performance (but not in the lines of code) is probably the result of this difference.

+3
Jan 08 '10 at
source share

I have a couple of tests that I did on the iPhone 3G almost 2 years ago, in those days there was no documentation or hard numbers. Not sure how valid they are, but the source code is sent and attached.

This is not a very extensive test, I was mainly interested in NSArray vs C Array for iterating a large number of objects.

http://memo.tv/nsarray_vs_c_array_performance_comparison

http://memo.tv/nsarray_vs_c_array_performance_comparison_part_ii_makeobjectsperformselector

You can see that the C array is much faster at high iterations. Since then, I realized that the bottleneck is probably not an iteration of NSArray, but sending a message. I wanted to try the ForSelector method and immediately call the methods to see how big the difference was, but it never got around. According to Mike Ash, it's a little over 5 times faster.

+3
Feb 06 '11 at 18:41
source share

This study says that to really get performance in a game with an intensive processor, you should use C. The related article is complemented by Xcode, which you can run.

I find the bottom line : use Objective-C, where you have to interact with iPhone features (after all, placing trampolines everywhere may not be good for everyone ), but when it comes to loops, things like vector classes of objects or intensive access to the array, bind to C ++ STL or C arrays to get good performance.

I mean, it would be foolish to see position = [[Vector3 alloc] init] ; . You simply request a performance hit if you use references to basic objects such as a position vector.

+1
Sep 07 '12 at 19:14
source share

Yes. C ++ dominates performance / expressiveness / resource compromise.

"I'm looking for hard data, not evangelism." google is your best friend.

  • obj-c nsstring is exchanged with C ++ by apple engineers for performance. on devices with limited resources, only C ++ cuts it as the MAINSTREAM language. NSString stringWithFormat slower

  • obj-c oop abstraction is deconstructed into procedural c-structures for performance, otherwise the MAGNITUDE order is slower than java! the author also knows about message caching, but hasn’t done so yet. therefore, modeling of a large number of small players / opponents is performed in oop using C ++ or, more, many procedural structures with a simple OOP shell around it with obj-c. there may be one paradigm that equates procedural + object-oriented programming = obj-c. http://ejourneyman.wordpress.com/2008/04/23/writing-a-ray-tracer-for-cocoa-objective-c/

-one
Jun 09 '13 at 13:20
source share



All Articles