473,770 Members | 4,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How expensive is brush creation?

I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?

Thanks for the info.

Tom P.
Aug 5 '08 #1
31 3415
On Tue, 05 Aug 2008 10:54:38 -0700, Tom P. <pa***********@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?
What's the speed difference when you measure your two proposed
implementations ? What performance problem are you trying to solve?

If the answers to those questions are "I haven't measured" and/or "no
particular performance problem", then you are premature in worrying about
the expense of creating a brush.

Pete
Aug 5 '08 #2
On 5 août, 19:54, "Tom P." <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?

Thanks for the info.

Tom P.
It is VERY expensive, not because of the brushes themselves, but
because you're feeding the Garbage Collector with all those brushes on
every Paint event.

You'd be better off using the system Brushes, which are static. Use:

Brushes.Black

instead of:

Brush b = new SolidBrush(Colo r.Black)

If you frequently use the same colors that are outside the 216
predefined ones, you can do this (in a static class, for instance):

private static Brush orangeyBrush;

public static Brush Orangey
{
get
{
if (orangeyBrush == null)
return (orangeyBrush = new
SolidBrush(Colo r.FromArgb(223, 156,0)));
}
}

This mimics what the framework does internally (lazy initialization) .

Michel
Aug 5 '08 #3
On 5 août, 20:01, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Tue, 05 Aug 2008 10:54:38 -0700, Tom P. <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?

What's the speed difference when you measure your two proposed *
implementations ? *What performance problem are you trying to solve?

If the answers to those questions are "I haven't measured" and/or "no *
particular performance problem", then you are premature in worrying about*
the expense of creating a brush.

Pete
Yes, I assumed "paint" meant "paint event". If it is "paint" as in
"image" or "picturebox " for instance, Pete's right, the improvement
might hamper readability for a negligible gain.

Michel
Aug 5 '08 #4
On Aug 5, 1:54 pm, "Tom P." <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?

Thanks for the info.

Tom P.
Hi,

I do not know for sure in managed world but in unmanaged a Brush is a
system ersource, so you have to be careful with it.
Why you need that many?
Aug 5 '08 #5
On Aug 5, 1:01*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Tue, 05 Aug 2008 10:54:38 -0700, Tom P. <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?

What's the speed difference when you measure your two proposed *
implementations ? *What performance problem are you trying to solve?

If the answers to those questions are "I haven't measured" and/or "no *
particular performance problem", then you are premature in worrying about*
the expense of creating a brush.

Pete
Nothing I've measured but the effort of redesigning my framework to
accommodate static brushes is no small feat. I am writing a file
manager and I custom paint each listview subitem (using
OnDrawSubItem). I allow custom colors and lineargradient brushes for
each subitem (name, size, and date). I have taken count and I'm
creating over 1000 brushes just to pull up a directory listing of the
root of my dev PC. Now I'm thinking about adding a color option for
Directories vs. Files (and maybe even readonly etc.) and so I'm forced
to wonder if the painting could get expensive.

Tom P.
Aug 5 '08 #6
On Aug 5, 1:12*pm, michel.desang.. .@gmail.com wrote:
On 5 août, 19:54, "Tom P." <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?
Thanks for the info.
Tom P.

It is VERY expensive, not because of the brushes themselves, but
because you're feeding the Garbage Collector with all those brushes on
every Paint event.

You'd be better off using the system Brushes, which are static. Use:

Brushes.Black

instead of:

Brush b = new SolidBrush(Colo r.Black)

If you frequently use the same colors that are outside the 216
predefined ones, you can do this (in a static class, for instance):

private static Brush orangeyBrush;

public static Brush Orangey
{
* get
* {
* * if (orangeyBrush == null)
* * * *return (orangeyBrush = new
SolidBrush(Colo r.FromArgb(223, 156,0)));
* *}

}

This mimics what the framework does internally (lazy initialization) .

Michel

I appreciate the help but I've got custom colors and lineargradient
brushes just littering this thing. As well as mouseover brushes and
now I'm kicking around the idea of Directory vs. File brushes and
maybe System or ReadOnly brushes. I've got to find a way to make them
permanent but I've also got the issue of creating the brush within the
correct bounds (what if the user changes the size of the column, I
need to resize the brush accordingly).

I'm gonna have to think this one through. Thanks.

Tom P.
Aug 5 '08 #7
On Tue, 05 Aug 2008 11:12:01 -0700, <mi************ **@gmail.comwro te:
It is VERY expensive, not because of the brushes themselves, but
because you're feeding the Garbage Collector with all those brushes on
every Paint event.
The GC design is optimized to handle repeated allocation and discarding of
short-lived objects. GC is not an issue here at all, though disposal of
brushes _theoretically_ might be. I doubt most code would see any issue.
You'd be better off using the system Brushes, which are static. Use:

Brushes.Black
Yes, very true. If one of the pre-defined brushes is adequate, using that
is much better than creating and disposing one.

Pete
Aug 5 '08 #8
On Aug 5, 1:22*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac... @gmail.comwrote :
On Aug 5, 1:54 pm, "Tom P." <padilla.he...@ gmail.comwrote:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?
Thanks for the info.
Tom P.

Hi,

I do not know for sure in managed world but in unmanaged a Brush is a
system ersource, so you have to be careful with it.
Why you need that many?
You bring up a very good point: what the heck am I doing? I made the
incorrect assumption that this would be a "yes/No" question and I'd be
done with it. My apologies all around.

I am making a file manager that paints each item in custom colors and
gradients at custom angles (lineargradient brushes). The mouseover
event will repaint the item with a custom mouseover brush. With each
subitem getting it's own paint event (OnDrawSubItem) this results in
~1,000 brushes for a simple 10 directory root drive.

Now I'm contemplating the addition of Directory Vs. File brushes and
even System vs. ReadOnly brushes. Hence the question.

Tom P.
Aug 5 '08 #9
On Tue, 05 Aug 2008 11:23:04 -0700, Tom P. <pa***********@ gmail.comwrote:
[...]
>What's the speed difference when you measure your two proposed Â*
implementation s? Â*What performance problem are you trying to solve?
[...]

Nothing I've measured but the effort of redesigning my framework to
accommodate static brushes is no small feat.
All the more reason to not do that unless you know for sure you have to.
I am writing a file
manager and I custom paint each listview subitem (using
OnDrawSubItem). I allow custom colors and lineargradient brushes for
each subitem (name, size, and date). I have taken count and I'm
creating over 1000 brushes just to pull up a directory listing of the
root of my dev PC.
1000 brushes? To display just one directory?

All due respect, I doubt the human brain is capable of comprehending a UI
presentation with that much variety in it.
Now I'm thinking about adding a color option for
Directories vs. Files (and maybe even readonly etc.) and so I'm forced
to wonder if the painting could get expensive.
Well, it _could_. Especially if just one directory causes you to create
1000 different brushes. But as long as things are performing adequately,
I don't think you should worry about it. Even if you do run into
performance issues, you might reconsider whether it's really necessary to
use so many different brushes. That is probably a better solution than
trying to cache thousands of brushes (as Ignacio points out, the unmanaged
resource in a brush is relatively scarce and so shouldn't be held on to
for long periods of time, especially in such large numbers).

Pete
Aug 5 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2355
by: Chris Lacey | last post by:
Hi, I'm currently writing a scheduling service which starts a number DotNet executables, each within a new AppDomain, every ten seconds. The guts of the code is as follows: // For each executable in the list of tasks for (int i = 0; i < this.processTasks.Length; i++) {
1
4863
by: Rene | last post by:
I would like to get a reference to a Brush using the color name of the brush. For example, right now if I want a brush with a certain color, I would hard code something like Brushes.PeachPuff, this will get me a PeachPuff brush, but what if my brush depended on a string passed by the user? If all I had was a string with the name "PeachPuff" how can I create Brushes.PeachPuff using the string value? Thank you.
2
43462
by: boxim | last post by:
having a moment, cant you just Brush br = new Brush(Color.Black);??? saying you cant cos it's an abstract class need to get to a brush from a color tia sam martin
5
2285
by: Dennis | last post by:
When I instantiate a Brush resource such as a solidbrush, do I need to dispose the resource when I'm finished or will it automatically be disposed when the function/sub it's used in ends? Thank you for any replies. -- Dennis in Houston
1
3861
by: =?Utf-8?B?UEtsZW1t?= | last post by:
how to serialize a brush object with the XmlSerializer without knowing the brush type?
2
1539
by: Johnny J. | last post by:
I've got an inherited control where I want the user to be able to specify a color property. Using that color, I'm drawing a line private m_UserDefinedColor as Color ..... Dim g as Graphics=this.CreateGraphics Dim myPen as Pen = new Pen(m_UserDefinedColor) g.DrawLine(myPen, X1, Y, X2 - 1, Y)
0
1281
by: steve | last post by:
When drawing with a hatch brush with a statement such as myGraphics.FillPolygon(hatchBrush, polygon.vertices); the hatch brush is always oriented in the same direction. Is there a way of rotating the brush so that I can give it a different orientation for each polygon? I.e. if it was a triangle and it was drawn upright then the brush would appear as normal but if the triangle was drawn upside down the hatch brush pattern would...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.