tags:

views:

19

answers:

1

Good night,

I have an SQLite query which I am executing in C# using the ExecuteScalar() command. The result in an Object type variable which is in fact a value of type Int64. As I need to cast it to an Int32, I tried first using Convert.ToInt32 and now I am using (Int32)(Int64)Object, although I get the same performance with both. The question is that this kind of cast seems to take an additional 6ms to perform, and the method concerned is critical code which should be executed as fast as possible... Is there any way to perform this cast faster, by using, for example, bit operators or some other method?

Just in case this is not possible, I really don't understand why the result of the query is an Int64... is there any way I can force it to be an Int32?

Thank you very much.

+1  A: 

You could use a data reader instead of execute scalar and call the static getint32 function. Not sure if it would be faster though.

Alternatively, you can cast your result in the sql query before it is returned. Again, this may actually decrease the overall performance.

wllmsaccnt
Thank you very much. The first suggestion works and is faster by a factor of 2-3x.
Miguel
I only mentioned it because I have reason to suspect that ExecuteScalar is just creating a datareader and returning the first object in the first row/column. This would mean it is doing one extra boxing operation as well. The difference should be fairly trivial, but I guess it isn't in this case. 6ms seems like a very long time to perform a double cast (with implicit conversion) on a boxed and then unboxed int64. Were you testing the speed inside of a test harness?
wllmsaccnt