|
I was wanted to share some performance observations after fiddling around with arrays and IList<type> . Since e.g. a double[] a array automatically implements IList<double> in .NET 2.0 I can conveniently accept IList<double> as argument for any method without also needing an overload for double[] . However, after some quick testing it seems a performance penalty is involved using the IList interface! I used this small program to compare the options:
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace TestListVsArray {<br />
class Program {<br />
static void Main(string[] args) {<br />
double[] a = new double[10000];<br />
<br />
for (int i = 0; i < 3; i++) {<br />
TestA(a);<br />
TestB(a);<br />
TestC(a);<br />
TestD(a);<br />
Console.WriteLine();<br />
}<br />
}<br />
<br />
private static void TestA(IList<double> a) {<br />
DateTime start = DateTime.Now;<br />
double x = 0d;<br />
for (int j = 0; j < 1000; j++) {<br />
for (int i = 0; i < a.Count; i++) {<br />
x += a[i];<br />
}<br />
}<br />
Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
}<br />
<br />
private static void TestB(IList<double> a) {<br />
DateTime start = DateTime.Now;<br />
double x = 0d;<br />
int n = a.Count;<br />
for (int j = 0; j < 1000; j++) {<br />
for (int i = 0; i < n; i++) {<br />
x += a[i];<br />
}<br />
}<br />
Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
}<br />
<br />
private static void TestC(double[] a) {<br />
DateTime start = DateTime.Now;<br />
double x = 0d;<br />
for (int j = 0; j < 1000; j++) {<br />
for (int i = 0; i < a.Length; i++) {<br />
x += a[i];<br />
}<br />
}<br />
Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
}<br />
<br />
private static void TestD(double[] a) {<br />
DateTime start = DateTime.Now;<br />
double x = 0d;<br />
int n = a.Length;<br />
for (int j = 0; j < 1000; j++) {<br />
for (int i = 0; i < n; i++) {<br />
x += a[i];<br />
}<br />
}<br />
Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
}<br />
}<br />
}<br />
Output on my machine was (tests are repeated to rule out exceptional starting behaviour):
<br />
00:00:01.1015840, 0<br />
00:00:00.6008640, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />
<br />
00:00:01.0915696, 0<br />
00:00:00.6008640, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />
<br />
00:00:01.0815552, 0<br />
00:00:00.6108784, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />
Concluding: iterating of the array interface is quite some faster than iterating over the IList interface of the same array object. Unfortunately using a.Count in the for loop also does cost some. I hoped the compiler/runtime was smart enough such that it would not cost anything.
Let's add those overloads again now.
Wout
|
|
|
|
|
Of course there will be a performance penalty for using a collection over an array. Even though with generics you can easily implement strongy typed collections (avoiding casting and boxing unboxing), the collections in fact use arrays. They provide you with more flexibility than an array (like you don't need to know the number of elements and they can grow as needed) but basically what it does is array operations.
There are some ways you can improve performance, if you know the number of items you can setup the MaximumLenght property of your collection.
Also, both with arrays and collections, whenever you try to access a member, the CLR will perform a bound check.
but if you access it in a for loop, where you expressely use the .Count, or .Lenght property, the compiler is smart enough to detect that and won't do bound checks with a considerable performance gain (in long running loops).
for example
suppose you have an array a and you want to iteract.
you can do it like this:
int limit = a.Lenght;
for (int i = 0; i < limit; i++)
{
}
this will do a bound check for each iteraction.
if you do
for (int i=0; i < a.Lenght; i++)
{
}
this boundcheck will not be performed.
|
|
|
|
|
The point is that I'm using a typesafe array in any case, not one of the collection flavors. I was just testing out which interface to the typesafe array was quickest. I would have hoped the difference between normal array access and access through IList<double> was minimal, but apparantly it isn't.
Also there seems no performance difference in practise between doing (see measurement results above):
int limit = a.Lenght;
for (int i = 0; i < limit; i++)<br />
{<br />
}
and
for (int i=0; i < a.Lenght; i++)<br />
{<br />
}
Maybe the compiler is smart enough here to optimize the limit variable away...
Wout
|
|
|
|
|
wout de zeeuw wrote: Maybe the compiler is smart enough here to optimize the limit variable away...
I'm not 100% sure but can't a.Length change inside the loop?
So therefore the compiler can't optimize it unless it analyses the whole loop, and I'm guessing that the problems that could occur if it did would outweight the speed advantage.
Ed
|
|
|
|
|
It can indeed (just tested )! So beats me why the two options are equally fast.
Wout
|
|
|
|
|
Because they're both accessing the same type of variable. The a.Length one is simply a property wrapper around an int so in effect they're doing the same thing and hopefully JIT will optimize away the property call if it's just a wrapper, but I'm not so sure
Ed
|
|
|
|
|
I am developing a program that will be used to present song lyrics and texts using a projector, much like Powerpoint can, but my program is specialized and not general like PP.
I have a basic prototype/testing bed ready, and am now starting to code the real program, and in the preparation I will have to decide wich features I will implement.
The program will be written i C# using Windows.Forms and GDI+ and will be released under the GPL license.
What I would like to know is this: I know that it is possible to embed an instance of the Media Player in a Form, but is it possible to use this as a background and then draw shapes and text over it, maybee using a transparent panel? Has anyone done this already?
I thought it was simplier to ask that to start trying it out, even if that is what I will do if I don't get an answer. My guess is that the biggest problem would be flickering from the transparent layer having to be redrawn constantly over the video, wich could maybe be taken care of with double buffering.
Yours Sincerely
/Rickard Haake
Bachelor of Science in Software development, but functioning as a
Youth Minister in the Mission Covenant Church of Sweden
-- modified at 5:05 Monday 20th March, 2006
|
|
|
|
|
Xaake wrote: but is it possible to use this as a background and then draw shapes and text over it, maybee using a transparent panel?
No, it isn't. The MediaPlayer will overdraw every control placed over the top of it. You'd have to use DirectShow and write a custom filter to put the overay test and images into the video stream.
Xaake wrote: wich could maybe be taken care of with double buffering
That would be true if Media Player gave you the opportunity to redraw your controls between every frame, but MediaPlayer doesn't expose such an event. So, no, you can't do this either.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Ok, Thanks for the answer.
Now I have to decide on either using MDX or sticking with GDI+ and skipping the video background feature
Yours Sincerely
/Rickard Haake
Bachelor of Science in Software development, but functioning as a
Youth Minister in the Mission Covenant Church of Sweden
|
|
|
|
|
Friends,
Is is possible in .NET to safely download and execute a code remotely ? (something like JINI in JAVA)? If yes, then how ?
Imtiaz
|
|
|
|
|
Hello,
I am developing my project, which is 3-tier architecture. I have a doubt in this architecture. As the Data Access component is unavailable to the UI layer, how can I work with the transaction states?
Because, as it is not possible to get the data access procedures are not available at UI layer, it is not possible at UI layer to Begin the transaction state and finally to commit the transation state.
Is it possible to handle the transaction state in the 3-tier architecture? I need your suggestions.
Thanks in advance.
With Regards,
Sheshadrinath.R
|
|
|
|
|
The best would be to handle the transaction entirey from the business layer, as it's all about business logic, not user interaction.
If that is not possible, you can create methods in the business layer for handling the transactions, and expose them to the UI layer. As the UI layer does not directly handle the transactions, it's an acceptable solution.
---
b { font-weight: normal; }
|
|
|
|
|
I am working on a binary clock program and have the logistics worked out and it "seems" to work. The only thing I am having trouble with is how to put it in the system tray over top of the windows clock. I guess I am looking for it to be a "skin" of sorts. So if I double-click on it, it still brings up the Date and Time. Anyone out there know where in the registry/system settings I can change it?
Bowlermonk
|
|
|
|
|
bowlermonk wrote: Anyone out there know where in the registry/system settings I can change it
[chuckles lightly] If only it was that easy! There's nothing in the registry that has anything to to with the clock, save for a single value. This value only specified whether the time will be displayed in 12hr or 24hr format. Nothing more...
Replacing the system tray clock is, by no means, easy. It involves finding the clock's window, getting it's thread, setting a hook to capture messages going to that window (which can't be done in Managed Code!), subclassing the window, and essentially bumping the real clock out of the way and taking over rendering of its window.
You can find a C++ (no .NET) example on SourceForge, I think it's called WinCalendarTime...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks for the quick respone. I guess I will be coming up with a different idea, rather than the system tray. I tell you, I love this site!
Bowlermonk
|
|
|
|
|
Quite possibly the best System Tray Clock replacement on the planet -> http://www.drive-software.com/[^]
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello all,
I am trying to create a video capture app using the windows media encoder9 sdk in visual studio 2003 .net a windows form project. using c++
and don't understand why this function call
CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&g_pEncoder);
will work perfectly fine in button1_click function (ie the g_pEncoder gets a value) but if I call the same function in the thread "mainloop" g_pEncoder comes back as not defined.
I have illiminated a lot of code ie: defines, includes etc. to save space.
Thanks for your time.
IWMEncoder2* g_pEncoder; //Global Variable
namespace TestingMyService
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private:
System::ComponentModel::Container * components;
void InitializeComponent(void)
{
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&g_pEncoder);
Threading::ThreadStart* threadstart = new Threading::ThreadStart(this,mainloop);
servicethread = new Threading::Thread(threadstart);
servicethread->Start();
}
public: static void mainloop(void)
{
CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&g_pEncoder);
}
|
|
|
|
|
Heyas all,
Is it possible, using the .NET framework, to programmatically determine if there is text in a textbox control or not. Say just a simple Bool value or something similiar. Basically I'm trying to build a query string dependant on what textboxes have text in them, if a textbox has text then include that text in the query if not, ignore completely. Is this possible or should I be going a completely different route?
Thanks,
John
|
|
|
|
|
Whatever textbox control you are talking about (web, windows or mobile), it has a Text property that you can use to get the text in the textbox. Simply check if the length of the string is zero or not.
---
b { font-weight: normal; }
|
|
|
|
|
That would work perfectly, thanks a lot guffa
Thanks,
John
|
|
|
|
|
Or simply check that the TextLength property is greater than zero.
--
I've killed again, haven't I?
|
|
|
|
|
TextBox1.Text.Equals("") may solve it
or use TextBox1.Text.Length
"Aim to go where U have never been B4 and Strive to achieve it"
http://groups.yahoo.com/subscribe/dotnetforfreshres
http://himabinduvejella.blogspot.com
|
|
|
|
|
Hello,
I am developing an application controlling various devices using a COM, dll or library.
The application is unique and must support any type of device. It is therefore built with all the dlls, COMs / interops that may be used.
However, only the dll, COM, library corresponding to the available device is installed when the product is deployed.
How can I prevent getting an error when the application starts because the unused dlls, (COMs + interops) or assemblies are missing, and then make sure I do not call them later on?
Thanks in advance for your help.
Michel
|
|
|
|
|
hi,
i have just downloaded sql ce from the microsoft website..but after installing it isn't working..nothing happens on my desktop let alone ppc. so is it required to have sql server for desktop b4 using sql ce? ..is there any tutorial on how to install or code that?
thanks..
|
|
|
|
|
SQL Server CE is an in-process database engine more like Microsoft Access than a standalone database server like the desktop SQL Server. Nothing much happens on your device until you deploy or debug your application, when Visual Studio will copy the device runtime components to the device.
I don't recall having to download anything separately from Microsoft before using SQL Server CE with VS.NET 2003. From what I recall the appropriate bits are installed when you install Smart Device support.
To create a new database, create a new instance of the SqlCeEngine class and call the CreateDatabase method. Don't forget to Dispose the SqlCeEngine object when you're done. You'll generally create your tables using SQL DDL statements; for this you'll need a SqlCeConnection , a SqlCeCommand , and use the ExecuteNonQuery method. Again, these objects need to be Disposed when you're finished using them. A using block (in C# and VB 2005) is very useful in ensuring you clean up properly since it automatically calls Dispose for you.
Programming for data access with SQL Server CE is very much like programming for the desktop - you simply use SqlCeConnection where you would use SqlConnection , etc. The SqlConnection class in Compact Framework is used for remote access to a SQL Server 2000 server, if you want to do that.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|