0
votes

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?

3
Can you please post (and format properly) real code? This cannot be correct. - Steve Townsend
Define "a lot of time." Are we talking microseconds or seconds? If the latter, then it's very likely that you're swapping memory to disk, and will only resolve with more RAM. 300,000 records is not that big, and simply accessing the memory for those 300,000 records should happen extremely quickly. - Anon
seconds. In each switch case I'm appending the value to an ostringstream. Would that take significantly longer? - aks
C or C++? I believe this answer can be very different for the different languages ... - pmg
@Rajesh: your code is not legitimate C or C++. The answer, however, depends. It depends on what you are doing with values, where you getting this block of memory from, etc. How do you know that switch statement is a bottleneck? Have you used a profiler? I simply cannot believe switch is a problem. - user405725

3 Answers

0
votes

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 switch statement. Note that:

  1. unions are usually used in C for such things to save space
  2. switch statements are very fast and translate to constant-time lookup tables
0
votes

If I understand your question correctly -- I assume you're going through each record, sortof saying "for each record i, if its type is 'char' then access the record at location i".

If you know how many records you have to access in advance, can't you just cache them all first?

If I'm completely off track forgive me for not understanding your point.

0
votes

Your comments finally gave enough information to answer the question: you're writing to an ostringstream. Which means that you're probably doing a lot of string manipulation inside your switch. Figure out how to optimize this, and your performance problems should go away.

(to convince yourself this is the case, simply comment out all code that references the stream and run your program again)