C# - Delay at Begining

N

NvrBst

I've noticed this with a lot of my C# program and been trying to get
ride of it for one of my programs recently. I have a program which
sends some key strokes to the active application when a hotkey is
pressed. The very first time you press the hotkey you can notice a
delay (150-250ms) before the keys actually gets sent; any hotkey
activations, after the first, result in instant results. Note: There
isn't any classes created on the 1st run that are used by the next
runs.

I suspect the delay is because the intermediate code (byte-code) is
getting compiled to machine code by the JIT compiler at runtime when
its first used and thus the small delay.

Is there any way to get ride of this for a C# program? A flag I can
set to have the program JIT all the byte-code when the program starts
up? Or maybe caching the exe so JIT can access it faster (if its not
already in RAM when you run the program). The program is very small
(50KBs).

I don't completely understand the delay or how it works on the inside,
and my search has been a little slow so far. Does the byte-code get
JIT on a class by class basis? If so creating the class's when the
program loads should get ride of the delay, but, if including the
internal .NET class's, even a small 50KB program can use a lot of
class's and would be tedious to implement this way. Would something
more elegant exsist?

Or maybe I'm way off the ball on the reasion for the inital delay?
Any information, knowledge, suggested links, or suggestions would also
be appresiated. Thanks

NB
 
S

Sergey Zyuzin

I've noticed this with a lot of my C# program and been trying to get
ride of it for one of my programs recently. šI have a program which
sends some key strokes to the active application when a hotkey is
pressed. šThe very first time you press the hotkey you can notice a
delay (150-250ms) before the keys actually gets sent; any hotkey
activations, after the first, result in instant results. šNote: There
isn't any classes created on the 1st run that are used by the next
runs.

I suspect the delay is because the intermediate code (byte-code) is
getting compiled to machine code by the JIT compiler at runtime when
its first used and thus the small delay.

Is there any way to get ride of this for a C# program? šA flag I can
set to have the program JIT all the byte-code when the program starts
up? šOr maybe caching the exe so JIT can access it faster (if its not
already in RAM when you run the program). šThe program is very small
(50KBs).

I don't completely understand the delay or how it works on the inside,
and my search has been a little slow so far. šDoes the byte-code get
JIT on a class by class basis? šIf so creating the class's when the
program loads should get ride of the delay, but, if including the
internal .NET class's, even a small 50KB program can use a lot of
class's and would be tedious to implement this way. šWould something
more elegant exsist?

Or maybe I'm way off the ball on the reasion for the inital delay?
Any information, knowledge, suggested links, or suggestions would also
be appresiated. šThanks

NB

Hi,

JIT compilation can be one of reasons. You can try 'ngen' utility
which comes with the framework
and precompiles the application to the native code.
There can be other reasons like static constructor invocations or lazy
initialization in .net framework classes
for example. I think you should use a profiler to determine the exact
reason if ngen doesn't help.

Thanks,
Sergey
 
N

NvrBst

Hi,

JIT compilation can be one of reasons. You can try 'ngen' utility
which comes with the framework
and precompiles the application to the native code.
There can be other reasons like static constructor invocations or lazy
initialization in .net framework classes
for example. I think you should use a profiler to determine the exact
reason if ngen doesn't help.

Thanks,
Sergey- Hide quoted text -

- Show quoted text -

Ahh thank you kindly :) I shall try ngen and some profilers :)

NB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top