I have a system that returns a void* to a memory block. This memory block stores contiguous data records of different types(int,char,double etc.) and gives the number of bytes of each field in each record.I essentially look up the type of the record and get the value of the record. To retrieve all the records, I do
switch(type)
{
case 'int' : \*(int*)(ptr+index)
case 'char': \*(char*)(ptr+index)
}
When I have to go through 300000 records this is taking a lot of time.Is there a faster way to go through all the records?
If a single block can be of multiple types that can only be resolved at runtime, you will have to dispatch to handlers in a
switchstatement. Note that:unions are usually used in C for such things to save spaceswitchstatements are very fast and translate to constant-time lookup tables