Code runs faster when launched from Linqpad vs from standalone executable
I've been hitting a strange issue whereby some code in an assembly executes faster (using about half the cpu power) when the code is launched from a linqpad query compared to when the code is started in a standalone executable.
So far, nothing I've tried seems to have brought parity to the two execution paths.
However, I think I may have narrowed down the probable cause to p/invoke executions, as commenting out the code with indirect native calls brings the execution speed up to the expected level.
Specifically, I think that the issue may be security stack walks on each p/invoke call due to the fact that the 'Total Runtime Checks' performance counter shows a significant (50k per second) increase in the standalone version, whereas the linqpad invocation demonstrates absolutely no change in this counter.
The native calls are also in a third party executable, so testing the SuppressUnmanagedCodeSecurityAttribute is difficult.
I can't find any documentation on how this counter is triggered, as all documentation for CAS seems to indicate that it is disabled by default as of .NET 4.0, and the caspol.exe tool no longer seems to support enabling or disabling this policy, and none of the app.config changes I've tried (looking at the linqpad ones) seem to have made any difference.
I'm wondering if linqpad performs any special operations during AppDomain setup that might affect the unmanaged code security checks, or really anything at all that could explain this difference.
Any comments or thoughts on what could be the cause here would be appreciated.