Ata
Ata

Reputation: 12544

how to make software faster

It seem that when I re run my .net application , it became much faster than before , why ?

Also is there anyway for my software to be run faster on startup ?

regards

Upvotes: 2

Views: 1295

Answers (3)

explorer
explorer

Reputation: 12090

The first time you run your .NET app the following happens:
1) Loading of your application, the runtime, and the framework from hard disk (which is slow) to the memory (which is much faster)
2) Then your application and the associated libraries are just-in-time JIT compiled to native code...as needed. This native code stays around in the memory, and the runtime infrastructure keeps a record of the code that it has compiled to native code.
3) Only in the third step does this native code actually executed by the processor.

If you dont shut down your computer and rerun your application. The following happens:
1) When the run time encounters your managed code that has already been compiled to native by the JIT compiler , it does not recompile it. It simply executes the already compile native in memory.
2) Only the code that was not JIT compiled to native in the first run is now compile from managed to native...and thats only if needed.

So on a second run of your application two things get real fast:
1) loading either doesnt happen at all or its far smaller than the first one.
2) compilation from managed to native either doesnt happen or its minimal

Thats why your second run of the application is almost always faster then the first run.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881443

This is almost certainly because the OS has loaded needed DLLs which stay in memory (unless the memory is needed elsewhere) after your application exits.

You can run your program in a special mode that just loads and exits) so that those DLLs will load up and this is a trick used by a few applications (MS Office and OpenOffice.org are two that spring to mind immediately).

Some people will run their programs at startup to make their first invocation seem faster but it's my opinion that this should be left to the user. It is their machine after all. By all means show them how they can do it (e.g., add yourprogram.exe /loadandexit to your startup folder) but leave it up to them.

I, for one, don't want every application I run slowing down my boot time.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

If it's the first .NET application running in your system, then the first time you run it, all the .NET libraries and the CLR have to be loaded from physical disk. The second time you run, everything will be in the file system cache, so it'll be loading it from memory. There may well be other caching effects in play beyond the file system cache, but that's the most obvious one.

The same is true of your specific application, although that's likely to be a lot smaller than the framework itself.

One option to try to bootstrap this is to have a small no-op application (e.g. a WinForms app that never actually launches a window) which runs on startup. Of course, this will slow down the rest of your startup a bit - and if the computer doesn't run any .NET applications for a long time, the framework will be ejected from the cache eventually.

Upvotes: 4

Related Questions