1

I want to store in an array of strings many lines of text(the text is always the same). I can think of 2 ways to so:

One way:

string s[100]={"first_line","second_line",...,"100th_line"};

The other way would be

string s[100];
fstream fin("text.txt");
for (int i = 0; i < 100; i++)
    fin.getline(s[i]);

text.txt:

first_line
second_line
...
100th_line

The actual number of lines will be around 500 and the length of each line will be 50-60 characters long.

So my question is: which way is faster/better?

L.E.: How can I put the text from the first method in another file and still be able to use the string s in my source.cpp? I want to do so because I don't want my source.cpp to get messy from all those lines of initialization.

3
  • "which way is faster/better?" Faster/better in what way? How often do you think these strings will be changing? Do you need to be able to change the strings while the application is running? Commented Nov 19, 2016 at 16:52
  • The strings will remain the same throughout the running of the application. I will only acces 20-30 of them(one time each), depending on user input. I am interested in which method is executed in less time. Commented Nov 19, 2016 at 17:02
  • There is a third alternative, use std::vector instead of a fixed capacity array. Commented Nov 19, 2016 at 19:41

4 Answers 4

2

Here some latency number every programmer should know:

    memory read from cache:          0.5-7 nanoseconds 
    memory read from main memory:      100 nanoseconds 
    SSD disk access:               150 000 nanoseconds (reach location to read)
    Hard disk access :          10 000 000 nanoseconds (reach location to read)

So what's the fastest for you ?

  • The first version will always be faster: the text is loaded together with your executable (no access overhead), and the string objects are constructed in memory (see assembly code online).

  • The second version will require several disk accesses (at least to open current directory, and to access the file), a couple of operating system actions (e.g. access control), not to forget the buffering of input in memory. Only then would the string objects be created in memory as in first version.

Fortunately, user don't notice nanoseconds and will probably not realize the difference: the human eye requires 13 ms to identify an image and the reaction time from eye to mouse is around 215 ms (215 000 000 nanoseconds)

So, my advice: no premature optimization. Focus on functionality (easy customization of content) and maintenability (e.g. easy localization if software used in several languages) before going too deep on performance.

Sign up to request clarification or add additional context in comments.

Comments

0

In the grand scheme of things, with only 500 relatively short strings, which approach is better is mostly an academic question, with little practical difference.

But, if one wants to be picky, reading it from a file requires a little bit more work at runtime than just immediately initializing the string array. Also, you have to prepare for the possibility that the initialization file is missing, and handle that possibility, in some way.

Compiling-in the initial string values as part of the code avoids the need to do some error handling, and saves a little bit of time. The biggest win will be lack of a need to handle the possibility that the initialization file is missing. There's a direct relationship between the likelyhood that something might go wrong, and the actual number of things that could potentially go wrong.

Comments

0

I'd go with the first one, since its constructing the string directly inside the array, which is practically emplace (Or perhaps its moving them, if so I might be wrong), without any more operations, so its probably much better than reading from hard-disk and then doing the same procedure as the first method.

Comments

0

If the data does not change then hard code it into a source file.

If you ever need to change the data, for testing or maintenance, the data should be placed into a file.

Don't worry about execution speed until somebody complains. Concentrate your efforts on robust and easily readable code. Most of the time spent with applications is in maintenance. If you make your code easy to maintain, you will spend less time maintaining it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.