|
Your test is invalid
for (int i = 0; i < 1000000; i++)
{
string s;
dict.TryGetValue(5, out s);
}
You never use the value of string 's' inside the loop. This means the compiler can optimize it out. Unless your running it in debug mode with debugger attached
The performance you get running in release mode without debugger attached can be massively different
This test is to trivial to be good measure of real performance.
Also your looking up the same key every time '5'. The performance can change significantly depending on what key you're looking up
|
|
|
|
|
Yes, I was running in debug with the debugger attached. I understand it is a trivial / not full scope test. However, I did the same test on all the various types I mentioned to get a *rough* idea of the differences before I implemented the real thing.
I think seeing that an int was 10ms and a struct was 590ms is pretty indicative the struct is not a good solution (without overriding the IsEquals and GetHashCode methods).
I did try hack in the _Key struct with the overridden IsEquals and GetHashCode methods into my real test application & class that does a lot with the values. I did not use the int key hacky thing I was questioning as that wouldn't work.
My original dictionary<type, someclass=""> took 110ms to run 1M iterations with the debugger attached and 3 items in the dictionary in the real application. Switching it to _Key and properly initializing the struct and the real hash code method bumped it up to 140ms to 150ms. So it actually added 30ms to 40ms of overhead which is what my trivial benchmarks kinda showed it would with 1M iterations.
FYI: Just for fun, I tried commenting out the IsEquals and GetHashCode overrides and it slowed to a crawl @ 1390ms! Wow. That is a lot more then I thought it would.
|
|
|
|
|
No probs. Was just making sure you're aware that release mode + no debugger can make substantial impact
on simple synthetic tests like this. But sounds like you are
I can't think of any other obvious way to improve the lookup performance further.
The inbuilt dictionary is pretty fast. It's often difficult to beat.
I've made some dictionary like structures that are faster GenericHashTrie<t> for example but they have significant downsides compared to the generic dictionary.
|
|
|
|
|
Don't use DateTime for measuring performance; use the Stopwatch class[^] instead.
As you suspect, your "breakthrough" won't work. If you use the result of GetHashCode as a key, you can incorrectly consider two different keys to be the same due to hash code collisions.
You could try making an immutable key class and caching the hash code:
sealed class _Key : IEquatable<Key>
{
private readonly string _s;
private readonly Type _t;
private readonly int _hashCode;
public _Key(string s, Type t)
{
if (s == null) throw new ArgumentNullException("s");
if (t == null) throw new ArgumentNullException("t");
_s = s;
_t = t;
unchecked
{
_hashCode = (s.GetHashCode() * 397) ^ t.GetHashCode();
}
}
public string S
{
get { return _s; }
}
public Type T
{
get { return _t; }
}
public override int GetHashCode()
{
return _hashCode;
}
public override bool Equals(object obj)
{
return Equals(obj as _Key);
}
public bool Equals(_Key other)
{
if (ReferenceEquals(other, null)) return false;
return _s == other._s && _t == other._t;
}
public static bool operator ==(_Key left, _Key right)
{
return Equals(left, right);
}
public static bool operator !=(_Key left, _Key right)
{
return !Equals(left, right);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
If you're really after performance - would it be possible not to use a dictionary at all?
Regards,
Rob Philpott.
|
|
|
|
|
Hello
I have an existing .NET application which is making use of RAPI.dll till date to sync data on windows mobile. It works in the following way.
On my .NET windows based application I have option to sync mobile. When I click on that option, my app connects to database fetches records and creates a .DAT file which will then be added to mobile with the help of RAPI.dll.
Now I was asked to make use of Microsoft Sync framework as part of architectural changes within the project, as it been used in other areas of the project. So Im just wondering to know how to get the similar functionality what we were having with RAPI.dll in our project.
Thanks in advance.
Krishna
|
|
|
|
|
krishnapnv wrote: Im just wondering to know how to get the similar functionality It probably won't work "similar". It's a complete framework, and it comes with a lot of documentation and examples[^].
You'll probably need to invest some time in those docs.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
my question is how to decode ( i dont have encoding method) :
i got these from a website :
d926ef0d-07df-3c5b-2246-12e061f71be1 ------> "0000000000"
fd7bd272-f7e8-1e57-0a82-e15b79694d25 ------> "09130000000"
146b2bf5-fddb-5e68-0e9a-e5cdd19513dc ------> "0000000001"
4fb94dee-654f-55a5-5c94-71974b23fcd4 --------> "09130000001"
b69ff890-e528-a2fc-c011-8f31dcfe4794 ----> "09125395974"
f734f107-2d2c-b2e0-ed26-b92679d7cf68 ---> ?
any decoding algorithm ?
thanks in advanced!
|
|
|
|
|
The values on the left are GUIDs and bear no relation to the values on the right. What is this information and what are you trying to achieve?
|
|
|
|
|
They look like GUIDs (they are formatted that way, but of course one can format any old garbage like that so you can't really be sure what they are). So there might not be any way to decode them. Perhaps, and this is just speculation, the strings are stored in a table and associated with a GUID when first encountered. That would mean you can't really do anything without access to that table. That's the sort of thing that's done to prevent whatever it is you want to do.
|
|
|
|
|
There may be, but more likely not.
Another possibility is that what is formatted as a GUID may simply be a 128-bit one-way hash of the value, in which case you can't "decode" it.
|
|
|
|
|
On the other hand, on the right side there are only 11-digit numbers. For 11-digit numbers, some 35 bits are sufficient (even when you encode it as ASCII characters, 88 bits are enough). If the process of generating the Guid uses that 11-digit number only (and nothing else) as an input, then a reversal of the process could be theoretically possible: no information need to be lost during that process. But that's surely not easy to find out how they did it.
|
|
|
|
|
Yes, but then the leading bits would probably be all zeroes.
|
|
|
|
|
Is it possible to have encoding method?
Thanks in advanced!
|
|
|
|
|
You are showing signs of optimism over experience: read the answers you have already, and try to think about what they are telling you.
I will summarize them for you:
Those look like GUID or Hash objects: neither of which is an encryption technique and neither of which can be decrypted as a result.
So there is no encoding method, and no decoding method.
And if you still can't work it out, here is an Executive Summary:
You
Can't
Do
That.
You looking for sympathy?
You'll find it in the dictionary, between sympathomimetic and sympatric
(Page 1788, if it helps)
|
|
|
|
|
I have seen this in this page>
it takes mobile and convert it to image and save it as file.
http://divar.ir/new/[^]
|
|
|
|
|
What has converting the image got to do with the GUID's you linked earlier?
Google translate refuses to translate the site. Can you explain me in simple terms what you're trying to achieve?
Giving a bunch of random numbers and asking for a conversion, will never work. The numbers could represent anything. Now, converting a mobile webpage, a pdf, or a document to an image, that's something we could help with.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
I have a one string in this format : abc 10 20 20 12 efg 10 20 20 13 prl 10 20 20 12 tyr 10 20 20 30 trop 10 10 20 20.
how can i split into new line as a
abc 10 20 20 12
efg 10 20 20 13
prl 10 20 20 12
tyr 10 20 20 30
trop 10 10 20 20
|
|
|
|
|
Use a regex:
[a-zA-Z]+(\s*\d+\s*)+
Should do it.
You looking for sympathy?
You'll find it in the dictionary, between sympathomimetic and sympatric
(Page 1788, if it helps)
|
|
|
|
|
If you need to split the alphabetic parts from the numeric parts, I'd recommend: (?'key'[a-zA-Z]+)(?'val'[0-9 ]+)
|
|
|
|
|
Hi All,
I develop a Windows form application and in the form KeyDown event
I use F8 key to do something, my problem is in any laptop contains the fn key
and F8 not work until you press the fn key, if any one click f8 only the event does not work.
Please let me know is it possible to make f8 and fn+f8 be the same functionality and how to do that in c#.
Thank You.
|
|
|
|
|
See which key is generated if you press "F8", and replace that key with it.
I'd recommend against it though. Your keyboard would suddenly work differently in your app, than compared to each and every app out there. It'd be more confusing than actually helping.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
in the BIOS you can probably swap the primary function of the fn key. i think it is called something like "action keys mode".
|
|
|
|
|
Good idea. I did that with my laptop - don't see the point in having a computer with the function keys 'disabled'.
Regards,
Rob Philpott.
|
|
|
|