Skip navigation.
 
mlRe: TextView question
FROM : Jason Jasmin
DATE : Fri Aug 11 19:33:42 2006

In response to help given by Spencer and Andreas...

I've tried the various options that have been presented so far, and 
haven't seen a noticable performance difference. When doing all the 
logic of adding my log message to the NSTextView, but without calling 
   scrollRangeToVisible:, it takes about 1ms per loop iteration (as 
deduced by looking at the timestamps on my NSLog calls in XCode). 
When I enable scrolling (either by calling scrollRangeToVisible: 
directly, or by cancelling the previous request as suggested by 
Andreas, it takes ~20ms per loop.

I'm still confused as to the difference in performance :/ Also, I had 
forgotten to mention earlier (I don't know if this will make a 
difference or not) but the work is being performed in a background 
thread, with the appendLog calls being made through a DO call.

What I've basically got looks like this:

// This is in main thread
- (oneway void)outputString:(bycopy NSString *)theString
{
   NSLog(@"%s    %@", _cmd, theString);
   [self appendLogText:theString];
}


- (void)appendLogText:(id)theText
{
   NSRange range;

   NSAttributedString *as = [[NSAttributedString alloc] 
initWithString:theText];
   [[logView2 textStorage] appendAttributedString:as];

   range.location = [[logView2 textStorage] length];
   range.length = 0;
   [logView2 scrollRangeToVisible:range];

//    [NSObject cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(scrollLogToEnd) object:nil];
//    [self performSelector:@selector(scrollLogToEnd) withObject:nil];

}

- (void)scrollLogToEnd
{
   NSRange range = NSMakeRange([[logView2 textStorage] length], 0);
   [logView2 scrollRangeToVisible:range];
}



I've tried various ways of appending the text, the second function 
reflects the latest version.

Is 20ms a reasonable time for updating the text in a loop? Also, in 
this latest version I'm jumping through some hoops to get a 
NSAttributedString, is there a better way that I'm missing? When I 
call the appendAttributedString: with a plain old NSString I get 
unknown selector errors, so I assume that the call is looking for 
something that NSString doesn't have by default.

Thanks,
Jason

On Aug 11, 2006, at 8:57 AM, Spencer Salazar wrote:

> Not sure if you tried this already, but I use [[logView2 
> textStorage] appendAttributedString:myString];  When I switched to 
> that (from NSTextView's appendString:) I got a huge speed increase, 
> up to about 20000 chars/500 lines per second (with scrolling to the 
> end).
>
> Also, if Im not mistaken, this line: NSString *newText = [theText 
> stringByAppendingString:@"\n"]; will copy the entirety of theText 
> into the new NSString, which can be really time consuming in a 
> tight loop.  Why not first append theText to your TextView (or 
> TextStorage), and then append a newline to the TextView (as the 
> TextView/TextStorage is more likely to be optimized for that sort 
> of thing than a String).
>
> spencer
>
>
> On Aug 10, 2006, at 11:48 PM, Jason Jasmin wrote:
>

>>
>> On Aug 10, 2006, at 3:42 PM, Andreas Mayer wrote:
>>

>>> What is "relatively slow" and what exactly (show some code!) did 
>>> you try?

>>
>> Sorry, I should have been more specific earlier... My problem is 
>> that in my code, for each loop iteration, I call NSLog twice, and 
>> write one line of output to my log window. If I disable all output 
>> to my log window, and just leave the NSLog calls, the app runs 
>> very quickly (on the order of 5 seconds or so). I can see the 
>> NSLog entries in XCode flying by quickly. When I enable output to 
>> my logging window, the app slows down dramatically, taking > 20 
>> seconds to run.
>>
>> Normally this wouldn't be a huge problem (I don't expect there to 
>> be that much output in production), but it's annoying because the 
>> XCode run log proves that the output can be a lot faster than it is.
>>
>> I've looked around the net for various "high performance" text 
>> solutions, and none of them are apparently any faster than what I 
>> had originally. Here's what I've got right now, it's the simplest 
>> text update code I could come up with:
>>
>>     NSRange range = NSMakeRange([[logView2 string] length], 0);
>>     NSString *newText = [theText stringByAppendingString:@"\n"];
>>     [logView2 replaceCharactersInRange:range withString:newText];
>>     [logView2 scrollRangeToVisible:range];
>>
>> Now if I comment the last line out (the scrollRangeToVisible) then 
>> it runs fast enough for me (it seems a little slower than with the 
>> entire logging function disabled, but it's in the same ballpark as 
>> XCode -- close enough).
>>
>> I've tried a few variations on that code, caching the length of 
>> the text to avoid calling NSString:length and so forth, but 
>> nothing makes any significant difference
>>
>> Jason
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Cocoa-dev mailing list      (<email_removed>)
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/spencer.salazar%
>> 40gmail.com
>>
>> This email sent to spencer.<email_removed>

>

Related mailsAuthorDate
mlTextView question Jason Jasmin Aug 10, 19:21
mlRe: TextView question Andreas Mayer Aug 10, 21:42
mlRe: TextView question Jason Jasmin Aug 11, 05:48
mlRe: TextView question Chris Suter Aug 11, 06:06
mlRe: TextView question Andreas Mayer Aug 11, 12:25
mlRe: TextView question Spencer Salazar Aug 11, 14:57
mlRe: TextView question Jason Jasmin Aug 11, 19:33
mlRe: TextView question Douglas Davidson Aug 16, 00:37
mlTextView Mai Bui Sep 15, 23:11