title
How to make your STRINGS FASTER in C++!
description
Sign up using my link to get 2 FREE MONTHS of Skillshare Premium! Click this link ► https://skl.sh/thechernoproject2
Patreon ► https://patreon.com/thecherno
Instagram ► https://instagram.com/thecherno
Twitter ► https://twitter.com/thecherno
Discord ► https://thecherno.com/discord
Series Playlist ► https://thecherno.com/cpp
This video is sponsored by Skillshare
detail
{'title': 'How to make your STRINGS FASTER in C++!', 'heatmap': [{'end': 324.676, 'start': 277.87, 'weight': 0.83}, {'end': 345.045, 'start': 324.896, 'weight': 0.776}, {'end': 408.318, 'start': 387.808, 'weight': 0.753}, {'end': 691.862, 'start': 664.933, 'weight': 0.819}, {'end': 704.498, 'start': 694.253, 'weight': 0.801}], 'summary': 'Learn to optimize string performance in c++ by understanding its impact on program performance and implementing techniques to make string operations faster. explore practical examples to improve performance, manage memory, and optimize memory allocations using stringview, reducing allocations and significantly impacting program performance.', 'chapters': [{'end': 142.139, 'segs': [{'end': 70.633, 'src': 'embed', 'start': 33.885, 'weight': 0, 'content': [{'end': 37.219, 'text': "We're going to be talking a lot more about this kind of stuff in the future.", 'start': 33.885, 'duration': 3.334}, {'end': 43.281, 'text': 'But today, I thought that we would actually just look at a really simple example of what you can do to make your strings faster.', 'start': 37.319, 'duration': 5.962}, {'end': 51.764, 'text': "When people deal with strings, I don't think they realize just the kind of impact a string has on the performance of your program.", 'start': 43.321, 'duration': 8.443}, {'end': 57.606, 'text': 'A lot of string operations in your program formatting text, logging stuff, getting into the right format,', 'start': 51.864, 'duration': 5.742}, {'end': 63.649, 'text': "using substring that kind of stuff is easy to overlook, because you know i'm just playing with a bunch of strings.", 'start': 57.606, 'duration': 6.043}, {'end': 68.311, 'text': "but no, playing with a bunch of strings is actually bad and it's very slow.", 'start': 63.649, 'duration': 4.662}, {'end': 70.633, 'text': "and we're just going to talk about one of the things that we can actually do.", 'start': 68.311, 'duration': 2.322}], 'summary': 'Optimizing string operations for faster program performance.', 'duration': 36.748, 'max_score': 33.885, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg33885.jpg'}], 'start': 0.029, 'title': 'Optimizing string performance', 'summary': 'Discusses optimizing string performance in c++ by highlighting the impact of strings on program performance and introducing a simple technique to make string operations faster.', 'chapters': [{'end': 142.139, 'start': 0.029, 'title': 'Optimizing string performance', 'summary': 'Discusses optimizing string performance in c++ by highlighting the impact of strings on program performance, introducing a simple technique to make string operations faster and promoting skillshare for creative and entrepreneurial learning.', 'duration': 142.11, 'highlights': ['The impact of strings on program performance is often overlooked, but string operations such as formatting text, logging, and using substring can significantly slow down programs.', 'Introducing a simple technique to make string operations faster is emphasized, highlighting the practical benefits of optimizing string performance in C++.', 'The promotion of Skillshare for creative and entrepreneurial learning is integrated into the content, offering a two-month free trial and emphasizing the availability of classes on programming, illustration, cinematography, and video quality improvement.']}], 'duration': 142.11, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg29.jpg', 'highlights': ['String operations like formatting text, logging, and using substring can significantly slow down programs.', 'Introducing a simple technique to make string operations faster is emphasized.', 'The impact of strings on program performance is often overlooked.']}, {'end': 388.348, 'segs': [{'end': 201.145, 'src': 'embed', 'start': 142.379, 'weight': 0, 'content': [{'end': 149.888, 'text': "I don't really want to sit here today and just talk for hours about why strings can be slow and the technicalities behind all of that.", 'start': 142.379, 'duration': 7.509}, {'end': 154.232, 'text': "I'd much rather just show you guys a practical example of what you can do to make them faster.", 'start': 149.908, 'duration': 4.324}, {'end': 161.02, 'text': 'But if I had to sum up, one of the main issues with std string and probably string formatting, string operations in general,', 'start': 154.553, 'duration': 6.467}, {'end': 163.424, 'text': 'is that they allocate memory.', 'start': 161.761, 'duration': 1.663}, {'end': 169.254, 'text': "If you guys aren't familiar with why memory allocation is bad, that's actually a really good topic for a video, by the way,", 'start': 163.444, 'duration': 5.81}, {'end': 172.881, 'text': 'that I definitely wanna make, but I do have a video about the stack versus the heap.', 'start': 169.254, 'duration': 3.627}, {'end': 178.896, 'text': 'definitely check that out if you want more information about just why allocating on the heap is not the best thing to do.', 'start': 173.294, 'duration': 5.602}, {'end': 184.079, 'text': "It's not that it's necessarily bad to allocate on the heap it's inevitable in a lot of cases, in fact.", 'start': 178.916, 'duration': 5.163}, {'end': 188.881, 'text': 'but where you can avoid it, you absolutely should, because it can slow down your program.', 'start': 184.079, 'duration': 4.802}, {'end': 195.503, 'text': "And std string and a lot of its functions love to allocate and that's not ideal.", 'start': 189.101, 'duration': 6.402}, {'end': 198.524, 'text': "In fact, let me just show you exactly what I'm talking about.", 'start': 195.703, 'duration': 2.821}, {'end': 201.145, 'text': 'So here we have a blank C++ program.', 'start': 198.624, 'duration': 2.521}], 'summary': 'Avoid memory allocation to make string operations faster.', 'duration': 58.766, 'max_score': 142.379, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg142379.jpg'}, {'end': 324.676, 'src': 'heatmap', 'start': 277.87, 'weight': 0.83, 'content': [{'end': 281.771, 'text': 'So now all of our allocations will flow through this new operator.', 'start': 277.87, 'duration': 3.901}, {'end': 284.612, 'text': 'It will count the amount of allocations we actually have.', 'start': 282.031, 'duration': 2.581}, {'end': 287.634, 'text': 'And also it will print just how big that allocation is.', 'start': 284.773, 'duration': 2.861}, {'end': 291.715, 'text': "And what I'll do at the very bottom here is also just print that allocations.", 'start': 288.034, 'duration': 3.681}, {'end': 294.097, 'text': 'Okay, cool.', 'start': 293.516, 'duration': 0.581}, {'end': 294.677, 'text': 'So there we go.', 'start': 294.157, 'duration': 0.52}, {'end': 296.518, 'text': "Let's run this program and see what happens.", 'start': 294.857, 'duration': 1.661}, {'end': 301.862, 'text': "And as you can see here, we've allocated eight bytes of memory and there has in fact been one heap allocation.", 'start': 296.838, 'duration': 5.024}, {'end': 304.403, 'text': "Now let's take a look at where that's come from.", 'start': 302.222, 'duration': 2.181}, {'end': 307.245, 'text': "I'll just take a break point over here and run the program again.", 'start': 304.564, 'duration': 2.681}, {'end': 307.746, 'text': 'Here we go.', 'start': 307.265, 'duration': 0.481}, {'end': 311.808, 'text': "The break point gets hit and you can see that it's coming from this line here.", 'start': 307.786, 'duration': 4.022}, {'end': 314.87, 'text': 'When we actually create a string that causes a heap allocation.', 'start': 311.848, 'duration': 3.022}, {'end': 319.854, 'text': "If we take a look at where it's coming from, it's coming from a std basic string of course.", 'start': 315.011, 'duration': 4.843}, {'end': 324.676, 'text': 'If we drill down a little bit, we can see that this in fact does allocate memory.', 'start': 320.294, 'duration': 4.382}], 'summary': 'New operator tracks 8 bytes of heap allocation, triggered by creating a string.', 'duration': 46.806, 'max_score': 277.87, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg277870.jpg'}, {'end': 314.87, 'src': 'embed', 'start': 288.034, 'weight': 4, 'content': [{'end': 291.715, 'text': "And what I'll do at the very bottom here is also just print that allocations.", 'start': 288.034, 'duration': 3.681}, {'end': 294.097, 'text': 'Okay, cool.', 'start': 293.516, 'duration': 0.581}, {'end': 294.677, 'text': 'So there we go.', 'start': 294.157, 'duration': 0.52}, {'end': 296.518, 'text': "Let's run this program and see what happens.", 'start': 294.857, 'duration': 1.661}, {'end': 301.862, 'text': "And as you can see here, we've allocated eight bytes of memory and there has in fact been one heap allocation.", 'start': 296.838, 'duration': 5.024}, {'end': 304.403, 'text': "Now let's take a look at where that's come from.", 'start': 302.222, 'duration': 2.181}, {'end': 307.245, 'text': "I'll just take a break point over here and run the program again.", 'start': 304.564, 'duration': 2.681}, {'end': 307.746, 'text': 'Here we go.', 'start': 307.265, 'duration': 0.481}, {'end': 311.808, 'text': "The break point gets hit and you can see that it's coming from this line here.", 'start': 307.786, 'duration': 4.022}, {'end': 314.87, 'text': 'When we actually create a string that causes a heap allocation.', 'start': 311.848, 'duration': 3.022}], 'summary': 'Program allocated 8 bytes of memory with 1 heap allocation.', 'duration': 26.836, 'max_score': 288.034, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg288034.jpg'}, {'end': 355.891, 'src': 'heatmap', 'start': 324.896, 'weight': 0.776, 'content': [{'end': 328.077, 'text': 'Okay, but what if we do something a little bit more simple?', 'start': 324.896, 'duration': 3.181}, {'end': 336.441, 'text': 'What if we just get rid of this string entirely and we just copy and paste this kind of const char data right into the print name function??', 'start': 328.198, 'duration': 8.243}, {'end': 337.862, 'text': 'Does that allocate memory??', 'start': 336.681, 'duration': 1.181}, {'end': 338.882, 'text': 'Yes or no??', 'start': 338.522, 'duration': 0.36}, {'end': 340.103, 'text': 'Write a comment below now.', 'start': 338.942, 'duration': 1.161}, {'end': 345.045, 'text': "I'm going to run this program and well, as you can see, it does in fact allocate memory.", 'start': 340.123, 'duration': 4.922}, {'end': 347.647, 'text': 'So if we go back here, this in fact does allocate memory.', 'start': 345.126, 'duration': 2.521}, {'end': 355.891, 'text': "We're still taking in a const reference here, of course, but it still needs to construct an std string for us and that construction allocates memory.", 'start': 347.667, 'duration': 8.224}], 'summary': 'Allocating memory by copying const char data into print name function.', 'duration': 30.995, 'max_score': 324.896, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg324896.jpg'}], 'start': 142.379, 'title': 'Improving string performance and managing memory in c++', 'summary': 'Discusses the impact of memory allocation on string operations, proposing practical examples to improve performance. it also highlights the impact of heap allocations in c++ programs and showcases memory allocation tracking using the new operator, with an example demonstrating the allocation of 8 bytes of memory.', 'chapters': [{'end': 178.896, 'start': 142.379, 'title': 'Improving string performance', 'summary': 'Discusses the performance issues with string operations and emphasizes the impact of memory allocation, proposing to showcase practical examples to improve performance.', 'duration': 36.517, 'highlights': ['The main issue with std string and string operations is that they allocate memory, impacting performance.', 'The negative impact of memory allocation on performance is highlighted, suggesting the importance of avoiding heap allocation for performance improvement.']}, {'end': 388.348, 'start': 178.916, 'title': 'Managing memory in c++', 'summary': 'Highlights the impact of heap allocations in c++ programs, demonstrating how std::string and its functions can lead to memory allocations, and showcases the use of the new operator to track and quantify memory allocations, with an example showing the allocation of 8 bytes of memory.', 'duration': 209.432, 'highlights': ['The chapter emphasizes the impact of heap allocations on C++ programs, advising to minimize its usage to improve program performance, especially with std::string and its functions.', 'The chapter demonstrates the use of the new operator to track and quantify memory allocations, showcasing an example that resulted in the allocation of 8 bytes of memory.', 'The chapter shows the allocation of memory when using std::string and its functions, highlighting the necessity to manage memory allocation in C++ programs to optimize performance.']}], 'duration': 245.969, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg142379.jpg', 'highlights': ['The negative impact of memory allocation on performance is highlighted, suggesting the importance of avoiding heap allocation for performance improvement.', 'The main issue with std string and string operations is that they allocate memory, impacting performance.', 'The chapter emphasizes the impact of heap allocations on C++ programs, advising to minimize its usage to improve program performance, especially with std::string and its functions.', 'The chapter shows the allocation of memory when using std::string and its functions, highlighting the necessity to manage memory allocation in C++ programs to optimize performance.', 'The chapter demonstrates the use of the new operator to track and quantify memory allocations, showcasing an example that resulted in the allocation of 8 bytes of memory.']}, {'end': 874.904, 'segs': [{'end': 440.352, 'src': 'embed', 'start': 408.338, 'weight': 0, 'content': [{'end': 412.18, 'text': "And if you've got some kind of real time running program, like an app or a game or something like that,", 'start': 408.338, 'duration': 3.842}, {'end': 417.041, 'text': "and you're doing this kind of stuff every frame, then it can really pile up and it can hurt your frame rate.", 'start': 412.18, 'duration': 4.861}, {'end': 422.957, 'text': 'So is there anything we can do to actually make this better for us? Yes, in fact, there is.', 'start': 417.182, 'duration': 5.775}, {'end': 427.701, 'text': "There's a very easy way that we can just modify this existing code to reduce the allocations to just one.", 'start': 422.997, 'duration': 4.704}, {'end': 432.786, 'text': "And what we'll do by the end of this video is get rid of all of these allocations.", 'start': 428.442, 'duration': 4.344}, {'end': 433.666, 'text': "We'll have zero.", 'start': 432.866, 'duration': 0.8}, {'end': 440.352, 'text': "If we just look at this program, whenever you're optimizing, you just wanna take a look at what it is you're trying to do.", 'start': 433.867, 'duration': 6.485}], 'summary': 'Optimizing real-time programs to reduce allocations and improve performance, aiming for zero allocations.', 'duration': 32.014, 'max_score': 408.338, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg408338.jpg'}, {'end': 691.862, 'src': 'heatmap', 'start': 664.933, 'weight': 0.819, 'content': [{'end': 668.154, 'text': 'we could just make it a C string, a const char pointer.', 'start': 664.933, 'duration': 3.221}, {'end': 671.375, 'text': 'So now instead of this name dot C string, we get rid of that entirely.', 'start': 668.274, 'duration': 3.101}, {'end': 672.976, 'text': 'The code becomes just that.', 'start': 671.615, 'duration': 1.361}, {'end': 678.758, 'text': "And then if I do run this program, look at that, isn't that beautiful? Zero allocations.", 'start': 673.076, 'duration': 5.682}, {'end': 684.86, 'text': 'Another thing to note here is that because I did change my print name function from being a string to a string view,', 'start': 679.098, 'duration': 5.762}, {'end': 691.862, 'text': 'that means that if I was to do something like I did before, where I just write print name and then just an actual string here like this,', 'start': 684.86, 'duration': 7.002}], 'summary': 'Code modification to use c string resulted in zero allocations. changed print name function to use string view.', 'duration': 26.929, 'max_score': 664.933, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg664933.jpg'}, {'end': 722.57, 'src': 'heatmap', 'start': 694.253, 'weight': 0.801, 'content': [{'end': 697.934, 'text': 'So if I run this program, You can see I get zero allocations.', 'start': 694.253, 'duration': 3.681}, {'end': 704.498, 'text': 'but if I go over here and I change this to be an STD string like this, even though it is a constant reference here,', 'start': 697.934, 'duration': 6.564}, {'end': 706.659, 'text': "I'll get rid of these two because they're string views.", 'start': 704.498, 'duration': 2.161}, {'end': 709.761, 'text': 'You can see that just by printing name channel, I of course get one allocation.', 'start': 706.819, 'duration': 2.942}, {'end': 715.905, 'text': "So even if you're not doing any kind of substring stuff, you know, all I want to do is have this print name function.", 'start': 709.821, 'duration': 6.084}, {'end': 722.57, 'text': 'Then I can take in a string view instead of a string, just by changing the signature of the function like this.', 'start': 716.286, 'duration': 6.284}], 'summary': 'Running the program results in zero allocations, but changing to a std string creates one allocation.', 'duration': 28.317, 'max_score': 694.253, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg694253.jpg'}, {'end': 750.331, 'src': 'embed', 'start': 723.01, 'weight': 1, 'content': [{'end': 727.393, 'text': "And suddenly I've gone from having one allocation to zero allocations.", 'start': 723.01, 'duration': 4.383}, {'end': 729.015, 'text': 'So in other words, to summarize.', 'start': 727.634, 'duration': 1.381}, {'end': 733.638, 'text': 'So we have two cases here, one which uses strings and one which uses string views.', 'start': 729.235, 'duration': 4.403}, {'end': 737.821, 'text': "Let's put them head to head and see what the total difference is.", 'start': 733.658, 'duration': 4.163}, {'end': 741.484, 'text': 'So for this example, I will revert this name to being a string.', 'start': 738.102, 'duration': 3.382}, {'end': 747.489, 'text': "Depends what the use case kind of is, but let's pretend that this data isn't just being statically typed like this in our C++ code.", 'start': 741.664, 'duration': 5.825}, {'end': 750.331, 'text': "Maybe it's coming in from a file or being generated somehow.", 'start': 747.529, 'duration': 2.802}], 'summary': 'Comparison of string and string view cases, allocation changes from one to zero.', 'duration': 27.321, 'max_score': 723.01, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg723010.jpg'}, {'end': 848.127, 'src': 'embed', 'start': 819.727, 'weight': 2, 'content': [{'end': 822.409, 'text': 'Try and put it into your game loop or something like that.', 'start': 819.727, 'duration': 2.682}, {'end': 830.193, 'text': 'Try and look into your own existing projects and start replacing a few of these strings with string views and see what kind of performance impact you have.', 'start': 822.929, 'duration': 7.264}, {'end': 831.974, 'text': 'Me. just speaking from experience.', 'start': 830.393, 'duration': 1.581}, {'end': 835.697, 'text': 'I know that if you look at pretty much any game and you profile it and you inspect it,', 'start': 831.974, 'duration': 3.723}, {'end': 842.208, 'text': "A sizable portion of that will be due to string operations and a lot of the times there's just not much you can do,", 'start': 836.832, 'duration': 5.376}, {'end': 846.326, 'text': "but of course There's always a little bit that you can probably get away with.", 'start': 842.208, 'duration': 4.118}, {'end': 848.127, 'text': 'Anyway, I hope you guys enjoyed this video.', 'start': 846.506, 'duration': 1.621}], 'summary': 'Optimize game performance by replacing strings with string views in existing projects for performance impact.', 'duration': 28.4, 'max_score': 819.727, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg819727.jpg'}], 'start': 388.748, 'title': 'Memory allocations and optimization with string views', 'summary': 'Delves into the impact of memory allocations on program performance, emphasizing three allocations and their impact on real-time running programs. it also explores optimizing memory allocations using stringview, reducing allocations from four to zero and then to one, with a significant performance impact on existing projects.', 'chapters': [{'end': 422.957, 'start': 388.748, 'title': 'Memory allocations in programs', 'summary': 'Discusses the impact of memory allocations on program performance, highlighting the occurrence of three allocations and the potential detriment to real-time running programs.', 'duration': 34.209, 'highlights': ['Real-time running programs, like apps or games, can suffer from a pile-up of memory allocations, potentially causing a decrease in frame rate.', 'The program in discussion incurs three allocations, indicating the potential impact of memory management on performance.', 'Memory allocations, if not managed efficiently, can have a detrimental effect on the frame rate of real-time running programs.']}, {'end': 874.904, 'start': 422.997, 'title': 'Optimizing memory allocations with string views', 'summary': 'Discusses optimizing memory allocations by utilizing stringview to eliminate unnecessary memory allocations, resulting in reduced allocations from four to zero and then to one, and highlighting the significant performance impact of replacing strings with string views in existing projects.', 'duration': 451.907, 'highlights': ['The chapter demonstrates modifying existing code to utilize StringView, reducing memory allocations from four to zero, and then to one, significantly impacting performance.', 'StringView enables creating a view into existing memory, eliminating memory allocations and reducing the memory footprint by just using a pointer and a size.', 'The speaker emphasizes the lightweight nature of passing a StringView around just by value, due to it being a pointer and a size, which contributes to a significant performance improvement.', 'The chapter highlights the performance impact of replacing strings with string views in existing projects, encouraging the audience to benchmark and experiment with the performance impact of utilizing string views.']}], 'duration': 486.156, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ZO68JEgoPeg/pics/ZO68JEgoPeg388748.jpg', 'highlights': ['Memory allocations can impact the frame rate of real-time running programs.', 'Utilizing StringView can reduce memory allocations from four to zero, significantly impacting performance.', 'Replacing strings with string views in existing projects can lead to a significant performance improvement.']}], 'highlights': ['Utilizing StringView can reduce memory allocations from four to zero, significantly impacting performance.', 'Replacing strings with string views in existing projects can lead to a significant performance improvement.', 'The main issue with std string and string operations is that they allocate memory, impacting performance.', 'The chapter demonstrates the use of the new operator to track and quantify memory allocations, showcasing an example that resulted in the allocation of 8 bytes of memory.', 'The negative impact of memory allocation on performance is highlighted, suggesting the importance of avoiding heap allocation for performance improvement.', 'The impact of strings on program performance is often overlooked.', 'Introducing a simple technique to make string operations faster is emphasized.', 'String operations like formatting text, logging, and using substring can significantly slow down programs.', 'Memory allocations can impact the frame rate of real-time running programs.', 'The chapter emphasizes the impact of heap allocations on C++ programs, advising to minimize its usage to improve program performance, especially with std::string and its functions.']}