Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Int vs Byte performance & SQL Int vs Binary performance

Tags:

c#

sql

In a C# windows app I handle HEX Strings. A single HEX string will have 5-30 HEX parts.

07 82 51 2A F1 C9 63 69 17 C1 1B BA C7 7A 18 20 20 8A 95 7A 54 5A E0 2E D4 3D 29

Currently I take this string and parse it into N number of integers using Convert.ToInt32(string, 16). I then add these int values to a database. When I extract these values from the database, I extract them as Ints and then convert them back into HEX string.

Would it be better performance wise to convert these string to bytes and then add them as binary data types within the database?

EDIT:

The 5-30 HEX parts correspond to specific tables where all the parts make up 1 record with individual parts. For instance, if i had 5 HEX values, they correspond to 5 seperate columns of 1 record.

EDIT:

To clarify (sorry):

I have 9 tables. Each table has a set number of columns.

table1:30

table2:18

table3:18

table4:18

table5:18

table6:13

table7:27

table8:5

table9:11

Each of these columns in every table corresponds to a specific HEX value.

For example, my app will receive a "payload" of 13 HEX components in a single string format: 07 82 51 2A F1 C9 63 69 17 C1 1B BA C7. Currently I take this string and parse the individual HEX components and convert them to ints, storing them in an int array. I then take these int values and store them in the corresponding table and columns in the database. When I read these values I get them as ints and then convert them to HEX strings.

What I am wondering is If I should conver the HEX string into a Byte array and store the bytes as SQL Binary variable types.

like image 420
Mausimo Avatar asked Dec 11 '25 22:12

Mausimo


1 Answers

Well in terms of performance, you should of course test both ways.

However, in terms of readability, if this is just arbitrary data, I'd certainly suggest using a byte array. If it's actually meant to represent a sequence of integers, that's fine - but why would you represent an arbitrary byte array using a collection of 4-byte integers? It doesn't fit in well with anything else:

  • You have to consider padding if your input data isn't a multiple of 4 bytes
  • It's a pain to work with in terms of reading and writing the data with streams
  • It's not clear how you're storing the integers in the database, but I'd expect a blob to be more efficient if you're just trying to store the whole thing

I would suggest writing the code the more natural way, keeping your data close to the kind of thing it's really trying to represent, and then measuring the performance. If it's good enough, then you don't need to look any further. If it's not, you'll have a good basis for tweaking.

like image 71
Jon Skeet Avatar answered Dec 14 '25 10:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!