Archive for the “.NET” Category
Posted by admin in .NET
I’ve been playing alot with reflection lately to analyze code and apply metrics on it. Unfortunately on large projects this means dealing with hundreds of thousands of objects representing classes, methods and the like. When parsing them I hit a wall because the code would run for hours before spitting out the results. I started looking for optimizations and one of the choices was to replace for cycles with linq queries. That added some benefits but still not enough.
What I saw was that my quad core processor was utilizing a single core and of course I started thinking threads but I am lazy. Luckily I remembered about PLinq. I haven’t used it until now so I did a little bit of research. Well research is a harsh word, more like reading a bit on how to use it and I found that is actually easy to switch a query from Linq to PLinq. At the most basic level you just have to select not from your collection but from your collection marked for paralelization. You can do that by saying myCollection.AsParallel(). Well ain’t that easy?!
Just so as you can see the importance of this, my processing went down from 5 hours to 1 hour.
You can see below some code that you can use to check the performance. Just be sure to run it on a multi core machine
Random r = new Random((int)DateTime.Now.Subtract(DateTime.Today).Ticks);
List<int> list = new List<int>();
for (int i = 0; i < 30000000; i++)
{
list.Add(r.Next());
}
DateTime t1 = DateTime.Now;
var paraleled = (from i in list.AsParallel()
where CheckPrime(i)
select i).ToList();
DateTime t2 = DateTime.Now;
var unparaleled = (from i in list
where CheckPrime(i)
select i).ToList();
DateTime t3 = DateTime.Now;
Console.WriteLine("Paralel linq select : " + t2.Subtract(t1).TotalMilliseconds);
Console.WriteLine("Normal linq select : " + t3.Subtract(t2).TotalMilliseconds);
One thing to highlight here is that the “slow” processing is emulated by a CheckPrime method which unfortunately is not as slow as I would want. If you want to see real benefits, modify to call a more intensive method there.
No Comments »
Posted by admin in .NET
No matter how experienced you are in ASP.Net, at some point you revert to google to remind yourself about the order of events in a page life cycle. Well, here it is (thanks Joe for summarizing it so nice):
If (!IsPostBack)
1. Begin PreInit
2. End PreInit
3. Begin Init
4. End Init
5. Begin InitComplete
6. End InitComplete
7. Begin PreLoad
8. End PreLoad
9. Begin Load
10. End Load
11. Begin LoadComplete
12. End LoadComplete
13. Begin PreRender
14. End PreRender
15. Begin PreRenderComplete
16. End PreRenderComplete
17. Begin SaveState
18. End SaveState
19. Begin SaveStateComplete
20. End SaveStateComplete
21. Begin Render
22. End Render
If(IsPostBack)
1. Begin PreInit
2. End PreInit
3. Begin Init
4. End Init
5. Begin InitComplete
6. End InitComplete
7. Begin LoadState
8. End LoadState
9. Begin ProcessPostData
10. End ProcessPostData
11. Begin PreLoad
12. End PreLoad
13. Begin Load
14. End Load
15. Begin ProcessPostData (Again)
16. End ProcessPostData (Again)
17. Begin Raise ChangedEvents
18. End Raise ChangedEvents
19. Begin Raise PostBackEvent
20. End Raise PostBackEvent
21. Begin LoadComplete
22. End LoadComplete
23. Begin PreRender
24. End PreRender
25. Begin PreRenderComplete
26. End PreRenderComplete
27. Begin SaveState
28. End SaveState
29. Begin SaveStateComplete
30. End SaveStateComplete
31. Begin Render
32. End Render
1 Comment »
Posted by admin in .NET
Chuck Norris jokes on .Net and computers:
- MVC actually stands for Model-View-ChuckNorris. Controller is just one of his nicknames.
- Chuck Norris was written in C# which itself was written in Chuck Norris
- You don’t follow Chuck Norris on Twitter. He follows you, finds you, and kills you
- The design of Silverlight DeepZoom was directly inspired by Chuck Norris’ powers of bionic vision.
- Chuck Norris doesn’t write code…oh no, he thinks about the finished product and the code appears.
- Chuck Norris has no need for virtual methods. Nothing can override Chuck Norris.
- A synchronize operation doesn’t protect against Chuck Norris, if he wants the object, he takes it.
- Chuck Norris invented recursion to see what would happen if he roundhouse kicked himself.
- Chuck Norris can multi-thread on a single processor by breaking it into pieces.
- Chuck Norris wrote a program that calculated the last digit of pi.
- Chuck Norris’ compiler is afraid of displaying warnings to him. It just fixes the code automatically.
- Chuck Norris uses Vista with UAC turned on. He has received no warnings. Ever.
- Chuck Norris monitor has no glare…no-one glares at Chuck Norris.
- The system works because Chuck Norris tells it to work
- Chuck Norris doesn’t need a test suite. The test suite needs Chuck Norris.
- CPUs run faster to get away from Chuck Norris
- Chuck Norris normalizes all schema just by inserting random data
- Packets travel faster than the speed of light for Chuck Norris, but he can still catch them
- Chuck Norris’s brain is his revision control, and it works better than git
- Chuck Norris can finish an infite loop in 1.3 seconds.
- Code written by Chuck Norris cannot be optimized.
- Chuck Norris never dies. He simply returns 0.
- Chuck Norris can break Moore’s Law
- Chuck Norris doesn’t need compilers nor editors. He roundhouse kicks the disk and the bytecode appears.
- Chuck Norris doesn’t use GOTO. Code comes to him.
- There is no theory of probability, just a list of events that Chuck Norris allows to occur.
- 90% of the worlds spam is handtyped by Chuck Norris. It takes him only 3 minutes.
- Chuck Norris can parse invalid XML
- Every time you don’t use “use strict” Chuck Norris kills a kitty.
- The best compression algorithm in existence are Chuck Norris fists.
- Chuck Norris can divide by 0.
- Chuck Norris can compile syntax errors
- The one true bracing style is the one Chuck Norris uses.
- Every program Chuck Norris has written can be run backwards. It will rollback whatever it did.
- No matter how you encrypt your traffic, Chuck Norris can read it by just looking at the cable. His ears can intercept wifi transmissions.
- Chuck Norris can enrich himself simply by hacking your bank account. He does not do this because there is no challenge in it.
- This blog does not contain a “Chuck Norris” category because Chuck Norris contains this blog.
1 Comment »
Posted by admin in .NET
No, I am not gonna talk about wages or market prices of your finished products. Instead I will talk a little about the fragility of our – read coders – situation.
We are basically on the top of the technological tree, using the most advanced tools to write top products for a technocratic society. We were inspired by movies about hackers, lured with wages above normal and compelled by the absolute control that we have on our creations. But we are so frail. We are dependent on the current state of technology. If any of the pillars would fall, we would face extinction.
Think about it. What do we need to do our jobs? We need electricity, high tech hardware and access to information. But these are the things that are more prone to disappear in case of calamities or war. Jeez, when you think that an earthquake can take out the whole powergrid; what do you do with your 800 watts hungry machine? Biking on a dynamo is out of the question. What will you do when an EMP strikes (ok, this might sound science fiction but so was a quad core cpu until a decade ago)? Will you write your 100 thousand software on a ENEAC card punching swallowing machine? What do we do when internet fails because of some foreign cybernetic assault?
We are maybe the most specialized field of work, and if we look into the history and evolution of the species, most highly specialized organisms were doomed to extinction at some point.
Can we survive a software development apocalypse? I don’t know and I ask myself sometimes “What do I do then”. Do I have any other skills? Can I build something with my bare hands? Can I sustain my family if I am not employed as a software developer?
Even worse, can I, or we, recreate the technological state of things if we need to restart anew? How, hard is it to create an integrated AND gate when you start from raw sand? And that is only if we can create reliable powersources and the surrounding ecosystems.
And then of course is the possibility of a science fiction future, where technology is king and AI is omnipresent but in a good way and all the above questions are for nothing … who knows, time will see but questions will still linger …
3 Comments »
Posted by admin in .NET
Today I worked on a strange issue where JQuery.Multifile was not working in other browsers than IE or FF. More exactly, it was not working on Opera 10.01, Google Chrome 3.0 and Safari. After googling and testing alot I was about to give up, especially when I saw this link: http://code.google.com/p/jquery-multifile-plugin/issues/detail?id=37
Still I was not ready to give up and dug deeper into the code until it hit me. The problem is related to the “name” property of the input tag. If you don’t specify it, in some browsers the files will not be sent along with the request.
Thus we have:
Bad way : <input type=”file” class=”multi” id=”fileControl”/> Works in IE and FF
Good way : <input type=”file” class=”multi” id=”fileControl” name=”fileControl”/> Works in IE, FF, Chrome, Opera, Safari and probably others (if they are based on WebKIT)
Cheers
No Comments »
Posted by admin in .NET
I am not gonna write a detailed entry today but I wanted to post some facts that might help someone:
WCF
Writing loosely coupled systems using WCF for communication purposes is a pain. First there is the service reference update which messes up xsd’s in relation with TFS. Secondly, when adding a reference in the client project it always set the address in the config file as being http://localmachinename.domain.local/service.svc instead of http://localhost/service.svc. While I understood the design, I really needed the localhost setting. Luckily I found out that if you set the endpoint address of the wcf server project to something specific to localhost (you don’t leave it empty as default) then when adding the reference in the client you get a localhost bound address.
Still, the contracts are reffered to as links to a specific machine and not your localhost. This means that developers getting the latest code have to update manually the refference and thus construct their own contracts.
IIS & Error Pages
I discovered a new setting in web.config in the customErrors tag: redirectMode.. By using it with the value ResponseRewrite, it is equivalent with using a Server.Transfer in the Application_Error handler of global.asax, preserving the error stack and additionally it preserves the url (looks nicer this way). Unfortunately from what I could gather, it’s IIS7 centric; I could not make it work on IIS6.
No Comments »
Posted by admin in .NET
Microsoft launched WebsiteSpark which basically is a sort of MSDN licence for developers but targeted only towards web development. What you get is:
- 3 licenses of Visual Studio 2008 Professional Edition
- 1 license of Expression Studio 3 (which includes Expression Blend, Sketchflow, and Web)
- 2 licenses of Expression Web 3
- 4 processor licenses of Windows Web Server 2008 R2
- 4 processor licenses of SQL Server 2008 Web Edition
- DotNetPanel control panel (enabling easy remote/hosted management of your servers)
And all this for free for 3 years. There are some limits still:
- Your company has to have less than 10 employees
- Your company must develop web applications for other clients
- You have to pay 100USD the moment you exit the program (either at the end of the 3 years or sooner)
There are some other things to be known but you can read it here.
No Comments »
Posted by admin in .NET
Today I tried to fix an automatic build on TFS. The build was failing because of the unit tests. The reason was simple but hard to figure. Let’s imagine the following scenario.
We have an interface IPet and we implement it in two dll’s, HousePets in a class Cat and in GardenPets in class Badger.
namespace HousePets
{
public class Cat : IPets.IPet
{ ... }
}
namespace GardenPets
{
public class Dog : IPets.IPet
{ ... }
}
namespace IPets
{
public interface IPet
{ ... }
}
And extra we have another class which is a loads an assembly and returns an implementation of IPet from one of the 2 dll’s.
public IPets.IPet GetCat()
{
Assembly catAssembly = Assembly.Load("HousePets.dll");
return (IPets.IPet)Activator.CreateInstance(catAssembly.GetType("Cat"));
}
Naturally this class is in a project that has no refference to either HousePets nor GardenPets.
If we have a unit test that uses it, in order for it to run, we just add the two projects as references to ensure that HousePets.dll and GardenPets.dll will be present in the bin directory of the unit test project.
While this works on the local machine, when ran in a TFS build environment it will crash. The reason is the static analysis that the TFS is starting. This is not detecting any object of type Cat or Badger and thus the libraries HousePets.dll and GardenPets.dll are not copied in the run directory.
The sollution is simple. Add a class in your unit project:
public class AssemblyInjector
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1804:RemoveUnusedLocals", MessageId = "c")]
public AssemblyInjector()
{
// make a call to a class of the library we want to be also copied in the
// test directory
HousePets.Cat c = new HousePets.Cat();
}
}
{ Disclaimer: don’t mind the implementation, it’s only there for illustrating the problem and the sollution }
No Comments »
Posted by admin in .NET
This weekend was all about Braidwood. For those not in the know, Braidwood is the reincarnation of Intel’s Turbo Cache memory. The buzz started with an article from Objective Analysis posted on InforWorld, here.
While the article was let’s say a nice report, it lacked on the technical part as the details on how it functions are very sketchy. This made me wonder what could you gain from using 16Gb of SLC memory on a mainboard. No, it is not intended to act as a harddrive and boot windows from it, but rather to act as cache memory between the harddrive and the system.
I guess we should look at the two operations a harddrive should perform, read and write. When writing the processor has most of the time the data in the memory and it sends it in chunks to the harddrive, this stores it in it’s cache memory and in mean time starts the spinning of the platters to begin the finding of a place to write the bits. Unfortunately this process is a long one, somewhere around 10ms, whereas the processor can get information from the memory under 1ms and thus at some point the processor has to wait for the harddrive to finish the writing and then it sends the next chunk. If we would have some more memory available on the harddrive, the processor could send more chunks and thus not wait until the harddrive finishes. This cache would be the SSD integrated in the mainboard, so we could see some improvements here, especially on many small files that need to be written. Of course for the user the feeling would be that yes, the files were written but the hdd led would still blink for a while, until the cache is emptied.
On the read part … well, there is not much where a greater cache can help, maybe only to store some very often used files such that reads on the harddrive are limited.
Somehow I don’t buy the panic the article starts spreading (OMG the SSD market will crash, there is no need for SSD’s anymore) . Jeez, while I think there are some improvements coming out, the harddrive is still an issue. It can only write so quick and read so quick. The initial begin of an operation will take the same amount of time, no matter the cache dimension or speed.
Also there is another big point to be made. While an SSD costs around 200$, to get the new turbo cache you would have to go through an upgrade to an I5 processor and a compatible motherboard, and that would set you down around 300$ and you still don’t get the same performance improvements.
Just my 2cents …
No Comments »
Posted by admin in .NET, tags: silverlight
These days I started looking into Silverlight and unfortunately my time was spent more on finding sollutions than actually implement something. I am so used to ASP and WinForms that somehow the new way of doing things in SilverLight is making me dizzy.
I started looking into DataGrid mostly and found two strange things:
- There is no double click event on a row
- The <enter> key is not caught in the OnKeyDown handler.
Of course there are solutions to this and based on some nice community posts I made my own implementation for the datagrid which is listed below
Read the rest of this entry »
8 Comments »
|