Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4116476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:42:46+00:00 2026-05-20T22:42:46+00:00

I am very new to Cocoa and this is probably a complete newb question.

  • 0

I am very new to Cocoa and this is probably a complete newb question. I am frustrated to death however.

I have an extremely simple Cocoa app (called “lines”) to test sending 1000 lines of text to a text view.

I started in Xcode 4 with “new Cocoa project” with all the defaults. This gives a blank window object upon which I can drag IB UI elements.

The UI I then constructed consists of a Text View and a button on the window of a NIB file. I am using Xcode 4 to drag those two elements to the .h file. The text view is connected to outView and the “1000” button is connected to one_thousand_button method.

The UI

Clicking the button “1000” triggers a loop to print 1,000 lines of text (“line 1\n” “line 2\n” … “line 1000”) to the NSTextView called “outView”

Here is the entire code (other than the .XIB file described):

linesAppDelegate.h:

#import <Cocoa/Cocoa.h>

@interface linesAppDelegate : NSObject <NSApplicationDelegate> {
@private
    NSWindow *window;
    NSTextView *outView;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextView *outView;
- (IBAction)one_thousand_button:(id)sender;

@end

linesAppDelegate.m:

#import "linesAppDelegate.h"

@implementation linesAppDelegate

@synthesize window;
@synthesize outView;

- (IBAction)one_thousand_button:(id)sender {
    NSString* oldString;
    NSString* newString;

    for(int i=1; i<=1000; i++){
        oldString = [outView string];
        newString = [oldString stringByAppendingString: 
                     [NSString stringWithFormat: @"Line %i\n",i]];
        [outView setString: newString];    
    }
}
@end

This is REALLY SLOW to execute. Perhaps 7 seconds the first time and increasingly slow with each press of “1000”. Even has the spinning colorful pizza of death!

I realize that this is probably not the right way to fill a NSTextView with 1,000 lines of text and that the loop that read the contents of the text view and appends that with stringByAppendingString method is the bottleneck.

What is the alternative method however?

Result

I wrapped this code:

    mach_timebase_info_data_t info;
    mach_timebase_info(&info);
    uint64_t start = mach_absolute_time();

// timed code    

    uint64_t duration = mach_absolute_time() - start;
    duration *= info.numer;
    duration /= info.denom;
    duration /= 1000000;

    NSLog(@"timed code took %lld milliseconds!", duration);

around the code from Adam Preble, my original, and drewk:

                 Adam Preble  (Adam, base)     drewk    my pitiful code
 1st run:           3ms          269ms         260ms      1,950ms
 2nd run            3ms          269ms         250ms      2,332ms
 3rd run:           2ms          270ms         260ms      2,880ms

The first run adds 1,000 lines; 2nd run adds another 1,000 lines, etc. (Adam, base) is his code without the beginEditing and endEditing

It is clear that using beginEditing and endEditing is WAY faster!

See the Apple documents on Synchronizing Editing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:42:47+00:00Added an answer on May 20, 2026 at 10:42 pm

    Wrap your updates to the text storage in calls to beginEditing and endEditing. This causes Cocoa to hold all of its change notifications until you have finished making your changes to the text storage.

    - (IBAction)oneThousandButton:(id)sender
    {
        NSTextStorage *textStorage = [outView textStorage];
        [textStorage beginEditing];
        for (int i = 1; i <= 1000; i++)
        {
            NSString *line = [NSString stringWithFormat: @"Line %i\n", i];
            [textStorage replaceCharactersInRange:NSMakeRange([textStorage length], 0)
                                       withString:line];
        }
        [textStorage endEditing];
    }
    

    On my system the above action runs in about 10ms. If I comment out the calls to beginEditing/endEditing, it takes about 470ms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping this is a simple question, as I'm very new to cocoa. I
I'm very new to Objective-C and Cocoa but I've made a simple app which
I think this is a very simple question, but I’m new to programming so
very new to signalR, and have rolled up a very simple app that will
Very new to python and can't understand why this isn't working. I have a
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
I'm building a (very) simple FTP app in Cocoa, and I need to store
I'm Delphi programmer and very new to Cocoa. at first I tried this :
Please let me preface this question with an apology. I am very new to
I have a very simple project. Extremely watered down. All it does is load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.