In .net, are calls to functions in the System namespace in general much slower than normal function calls? I know that, as a general fact, "System calls are slow." However, in this case, I am concerned not with system calls but with calls to the "System" namespace.
For example, consider the situation where I have an array of 10000 sentences, and I want to figure out which of them start with the 开发者_Go百科word "computer." Would I be better off making 10000 calls to the System.Text.RegularExpressions
namespace? Or would it be significantly better to implement a
StartsWithPrefix(ByVal prefix as String, ByVal str as String) As Boolean
function?
Granted, regexes are slow in their own right, but I think that my question is still meaningful.
No. The System.xxx namespace is just a namespace, nothing more.
'System calls are slow' is shorthand for "Calls from unprivileged user-mode code into privileged operating-system code suffer some significant overheads on the way in and out of the privileged parts of the operating system."
That is not the same as "Using the word 'system' in my function names makes them run more slowly."
No, there's nothing specific about calls to methods in types in the System namespace that makes them slow.
If you want to find strings starting with "computer", I'd use
if (foo.StartsWith("computer"))
You should consider what sort of prefix comparison you want though - for example, if you use:
if (foo.StartsWith("computer", StringComparison.Ordinal))
that may well be much faster - but not culture-sensitive. I don't know whether that matters in your specific case, but you should think about it.
Code in the System
namespace is just the very same code as in any other namespace. Namespace does not affect the nature or performance of the code. And most of the System
namespace doesn't have to do with calling into the OS or something of that nature. It's just a bunch of utility classes. Runtime library, if you will.
As for telling if a string starts with a given prefix, you can use the String.StartsWidth
method.
精彩评论