Sometimes what you think are hard constants aren't! This is a late stage code optimization, and in the case of an ID and std::string, it's safer and more readable to start with that and only then consider changing it later. However, even that doesn't matter until after your code runs the way you want it to. You never have to worry about a second fetch being needed just to get information for a single temporary struct. So, if you know an object ID is always going to be 10 characters, then std::array might be more complicated to work with than a std::string, but it's guaranteed that it will be right there with all of your other struct data. Oh, relating to your original question, a cheap optimization is have small data be heap allocated rather than stack. A good IDE (I prefer Clion) should earn you about things like alignment, so just pay attention to the warnings. As I said, unless you're going for something specific or doing high performance computing it doesn't matter. This makes stack memory a bit faster in practice compared to heap memory. These optimizations work very well on stack memory, since it contains local variables that you are using the most, the memory you are using is close together, plus you are repeatedly accessing the same data over and over. For example, they use spacial and temporal optimization, since when you access an element in an array, chances are you are also gonna access the next element in the array. But modern CPUs are very complicated but well optimized, making common use patterns faster. But once the memory is allocated, the access time is in theory the same. Now since heap is dynamic, it needs to be managed. The values in that function might change, but the size of the function can't. This is because every function needs to have a fixed amount of stack memory it will consume. depending on runtime input, it can not use memory regions with sizes based on runtime values. So while a program only using stack memory can do must things like call different functions, take different if branches etc. Actually the entire program could be run compile time, but as you might already think yourself, that doesn't make any sense. Stack memory is static, that means that if a program ran only using compile time constants, you could calculate the exact memory location of every value up front. But since programs don't operate in a vacuum, there are differences. One important detail that might seem nitpicky but is good for understanding this whole memory thing:Īccessing memory from heap and stack, in a vacuum, is equally fast, just the allocation is slower. if your post does not appear in the new queue, just send a message to the moderators.make your questions relevant to other readers.give your post a meaningful title, i.e., NOT "I have a C++ problem" but, e.g., "Problem with nested for loops".thoroughly research for an answer first.Tips for improving your chances of getting helpful answers: Read these guidelines for how to ask smart questions.įor learning books, check The Definitive C++ Book Guide and Listįlair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page. Hasty-sounding questions get hasty answers, or none at all. New to C++? Learn at READ BEFORE POSTINGīefore you post, please read our sticky on proper code formatting. For general discussion and news about c++ see r/cpp. This is a subreddit for c++ questions with answers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |