|
True, but IIRC native code is still faster than unsafe.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
I'd be surprised at that. And given the P/Invoke overhead that would be required with a C++ lib on the side, unsafe C# may quite well out-perform.
|
|
|
|
|
I'm not sure if this counts or not, there'e //unsafe comments in the C# source, but I thought you needed an unsafe keyword as well which I didn't see. It does have c++ scoring better in most math related benchmarks.
Bencharks[^]
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
Yeah, it looks like he commented the unsafe portions, meaning he's back to normal array bounds checking and all that. I wonder why?
|
|
|
|
|
There always will be a loop either in your code, or in code you call.
100K samples will not be processed by a linear sequence of instructions !
You can improve performance by using pointers; this is possible in C# too,
it is considered unsafe, and requires the "unsafe" keyword (and a compiler switch
to enable it).
Look in Visual Studio's help for "unsafe code, sample [C#]"
Luc Pattyn
|
|
|
|
|
One way to speed up things like this on multi-processor or multi-core machines is to use multiple threads to do the processing. Say you have one of those new Intel Quad Core processors, you've got 4 hardware threads available. Split the array into 4 segments, then spawn 3 new threads, each the processes a segment of its own. Then have the current thread process a segment of its own (4 threads total). You've essentially increased performance 4x.
Of course, this solution only works for processors that are hyperthreaded (i.e. 2 hardware threads per physical processor core) or for machines that have multiple processors or multiple cores.
p.s. one should always question optimizing code like this unless you're absolutely, positively certain that there is a performance bottleneck here and the current performance is not acceptable.
|
|
|
|
|
For a job as simple as an inner product, I expect bus bandwidth limitations will
be dominant over CPU limitations, so no much help from multi-threading...
I do fully agree with the p.s. though
Luc Pattyn
|
|
|
|
|
The job is simple, but the bottleneck is the linear time it takes to compute one operation, move onto the next, until finished. Meanwhile, potentially 1 or more cores are idle and could be doing these operations in the meantime. I'm quite certain you'd see a good speed up here.
The MS Robotics team that built the CCR (concurrency and coordination runtime, a .NET library for threading and coordination among threads) found big speedups, often near a multiple of the number of cores in a machine, by utilizing multiple threads to do this kind of thing. Given, they are doing lots of IO, however.
Joe Duffy, a CLR architect, is busy working on the PLinq (Parallel Language Integrated Query) project that will allow devs to easily parallelize queries and transformations on data. This is essentially the technique I described above: using 1 thread per hardware thread to parallelize queries and transformations on data. Eric Sink has an article[^] on his blog showing how C# can do Map, which utilizes this idea.
|
|
|
|
|
Sure, I believe multithreading can be great for any single job that is compute bound
(including Map, the CCR stuff, and much more), as well as for most situations where a multitude of jobs come together.
But my point is multiplying two arrays isnt much more than a data mover.
And I expect the loop overhead will mostly be dealt with by the CPU's out-of-order
capabilities. So lets wait and see.
Luc Pattyn
|
|
|
|
|
There is a library at Microsoft Research called Accelerator that might be worth investigating.
Accelerator provides a high-level data-parallel programming model as a library that is available for all .Net programming languages. The library translates the data-parallel operations on-the-fly to optimized GPU pixel shader code and API calls. Future versions will target multi-core cpus.
Download, Channel9 Video
|
|
|
|
|
Bet you didn't know you would get all this 'feed back' did you
Just remember that no matter what you will still have the loop. So, think of it like this do I have a loop here with a little code or do I have a loop somewhere else with extra code to get there?
Your call, but these guys are giving some real good knowledge that you should diffently try to learn.
Good Luck,
Jason
Programmer: A biological machine designed to convert caffeine into code. * Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
You can multithread the multiplication if the arrays are large enough. Another option is to drop into unsafe code and use pointer arithmetic.
Antoher option is to use 64 bit multiplication but you might need a bitwise transform on the result. (Check out the the assembly for strlen for a non-application example)
I don't know about intel chips but some processors provide op codes for array based operations. Check the MMX instruction set and you may be able to multiply the entire set in two or three op codes.
http://web.cs.wpi.edu/~matt/courses/cs563/talks/powwie/p3/mmx.htm
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Hi coders
Just wanted to say thanks for all your many inputs - As expected there seems to be no magic answer to this quistion, but I will take a closer look at using pointers - I did not know this was possible in C# so thanks alot...
AL
|
|
|
|
|
hello every one,
well I was just making a windows form here and it has a column that displays a currency column. SO I know I can change the style of the column to represent a currency but by default it's showing the $ sign while I want it to display the Ruppee symbol like Rs. or something. So can any one of u tell me how to do it. It'd really be v nice u know.
thanks in advance,
Rocky
|
|
|
|
|
There are a number of ways to accomplish this. The easiest may be to change the culture and let the framework handle it. You could also apply a custom format in a data binding expression, or even tranform the column in the source before it is even bound to the datagrid.
only two letters away from being an asset
|
|
|
|
|
ahh by the way, can u tell me how do we change the culture??
and plz can u elaborate this other alternative of customizing the format of the data binding expression...
thanks.
Rocky
|
|
|
|
|
|
I am new to C# and I am having some difficulty in the following task. I have a MDI parent form that calls a child form. I need to pass a reference to an object that is instantiated in the MDI parent. Should this be passed as parameter to the child form constructor? Is there a better way?
Basically I need to create the object in the parent form and have access to the same object in the child form. If the child form makes a change to the state of the object I need the parent form to have access to that change.
Thanks
|
|
|
|
|
There are many ways you could accomplish this. Having a separate property or set it in the constructor are both fine.
The question is, what do you want the object to do?
Does the child form set properties, call methods on the object?
Should this be modelled using the Model View Controller pattern?
What will the parent do with the changed object?
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
The child will change properties and call events on the object. The object is a basic business object that opens, reads and writes to text streams. Properties of this object deal with file name, creation time, etc. Methods are, SaveFile(), OpenFile(), ReadFile().
I am not familiar with the Model View Controller pattern, I will look that up. What I am trying to get is the basic calling syntax. I seem to be having problems with passing an object reference to the child form constructor.
Thanks again
|
|
|
|
|
This has already been done for you, take a look at the Form.Owner property.
I have no idea what I just said. But my intentions were sincere.
|
|
|
|
|
Thanks, I will check this out.
|
|
|
|
|
Hi Everyone,
Some of the .net dlls inh our application has to be shared with some client applications... So we are installing them in GAC , after strong naming them... However we are facing an issue. whenever we are changing the dlls installed in GAC, (We are not version number and snk), we have to rebuild all the application that use this file even id there is not apparent interface change... What are we doing wrong....
Regards
Sabarish
|
|
|
|
|
To share a dll, you do not have to install it into the GAC. Have you tried just copying the dll and referencing it in the other application?
As far as referencing assemblies in the GAC, you can click on a reference in your project (whether a reference to a GAC assembly or a non-GAC'd assembly) and specify whether the app requires a specific version.
Does that help?
|
|
|
|
|
Hi Judah,
The first otpion does not suite with the client, they do not want any of our dlls where their application is installed.... our will be in 1 folder and theris in another...
The second option we had tried, but the issue is that we have toi change in about 50 project
|
|
|
|