I have found that time of the string colouring depends on how many different NSColors are used. In code below if I use only one colour for the three cases then the text colouring process is 3 times faster than in the case when three different colours are used for these three cases, each colour for each case. Why ? Is there a way not to slow down the colouring for three different colours ?
for i in 0..<arrayOfNSRangesForA.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForA[i])
}
for i in 0..<arrayOfNSRangesForT.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForT[i])
}
for i in 0..<arrayOfNSRangesForC.count
{
textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForC[i])
}
Update
I have found one more BAD thing. When I changed colouring from NSForegroundColorAttributeName
to NSBackgroundColorAttributeName
the running time has increased significantly - 10 times. For 20 000 characters, it was for one colour, for NSForegroundColorAttributeName
- 1 sec, for NSBackgroundColorAttributeName
- 10 sec; if three colours - 3 and 30 sec accordingly. For me it is very bad feature of Swift !!! It is not possible to do normal work with DNA (ATGC sequence) colouring, since the length of DNA is thousands of A,T,G,C characters!
Update In comments I have a suggestion to colour only visible part of text. I have tried this approach and it is much worse even for shorter text in comparison with what I did in standard way. So, I had NSRange of text for visible part of text, and did colouring on fly while scrolling by using notification when scrolling is on. It is a bad way.
enumerateAttributes(in:options:using:)
to see how many runs there are for each case. – TelegenicNSMutableAttributedString
coalesces attribute runs. It's possible that each time attributes are changed for a range, that it checks if the immediately preceding or following characters share the new attribute set. If they do, it could coalesce them into one run. Or it could use some other approach. – TelegenicNSForegroundColorAttributeName
toNSBackgroundColorAttributeName
, (rest of code the same) colouring time has increased significantly. Fro 20 000 characters from 3 sec to 30 sec. – IrreligiousNSAttributedString
is intended for managing formatted text for display on the screen. It is not designed to be good at manipulating DNA sequences. Are you using this class to display enormous DNA strings? – CrozierString
, and it is working well, notNSAttributedString
. Only to colour the final resultingString
I am usingNSAttributedString
, exactly for purpose to display it on the screen in colour. – IrreligiousString
in colour, rather than converting it toNSAttributedString
? – IrreligiousNSAttributedString
is not performant enough for you, you may be able to drop down a level to the Core Text framework. Or, another option might be to build theNSAttributedString
in a background thread, so it doesn't slow down the main thread. Or color just the visible portion of the string at a time. It depends on your situation. – Crozier