1 00:00:07,230 --> 00:00:07,590 Hello. 2 00:00:08,190 --> 00:00:12,020 In this lecture, we're going to talk about Topic 48. 3 00:00:12,480 --> 00:00:18,400 What is the difference between debug and release builds when using Visual Studio? 4 00:00:18,420 --> 00:00:22,620 It's hard not to notice this little option in the top menu. 5 00:00:23,700 --> 00:00:29,910 When developing the code, we most likely use Ladybug mode, and we don't really think much about the 6 00:00:29,910 --> 00:00:30,750 other option. 7 00:00:30,990 --> 00:00:38,520 The release mode as their name suggests, the debug mode is most appropriate when debugging the application, 8 00:00:38,730 --> 00:00:45,750 so mostly during the development and the release mode can be used when we intend to release the application 9 00:00:45,840 --> 00:00:49,200 so it can be used by users or other programs. 10 00:00:49,680 --> 00:00:52,230 But what is the difference between them exactly? 11 00:00:52,800 --> 00:00:59,340 The main difference is that in the released build, the compiler introduces some optimizations it finds 12 00:00:59,340 --> 00:01:00,180 appropriate. 13 00:01:00,570 --> 00:01:06,000 They can be things like removing unused variables or simplifying method calls. 14 00:01:06,330 --> 00:01:12,480 It helps to make the final common intermediate language code smaller, simpler and often faster. 15 00:01:12,960 --> 00:01:20,130 Remember that the common intermediate language is the language to which the compiler compiles this code. 16 00:01:20,790 --> 00:01:27,240 Those optimizations shouldn't be done in the debugging mode because they can make debugging harder as 17 00:01:27,240 --> 00:01:30,960 the compound result doesn't match exactly the source code. 18 00:01:31,530 --> 00:01:35,040 Let's see the simple compiler optimizations in practice. 19 00:01:35,400 --> 00:01:38,700 First, let's see some code that is not optimal. 20 00:01:39,120 --> 00:01:41,190 This variable is not used. 21 00:01:41,760 --> 00:01:44,910 First, I will build this project in the debug mode. 22 00:01:48,390 --> 00:01:51,900 The result of compilation should end up in the Dimmock folder. 23 00:01:52,440 --> 00:02:00,000 The fastest way to go down is by right clicking on the product and selecting Open Folder in File Explorer. 24 00:02:03,850 --> 00:02:06,040 Let's find the compound Diallo. 25 00:02:07,180 --> 00:02:16,570 It is in the Dybbuk folder, a six pack that would I'm going to do now is to first use IL dozen to see 26 00:02:16,570 --> 00:02:18,640 the common intermediate language code. 27 00:02:19,000 --> 00:02:25,240 Then we will translate this code back to C-sharp to see how the compilation affected the structure of 28 00:02:25,240 --> 00:02:25,870 the code. 29 00:02:26,680 --> 00:02:30,190 You can find it does an application under such path. 30 00:02:30,910 --> 00:02:31,690 Let's run it. 31 00:02:34,030 --> 00:02:38,050 Now we can simply drag that yellow and drop it to your Dawson. 32 00:02:40,510 --> 00:02:45,940 To save the common intermediate language called in the file, we must select, file and dump. 33 00:02:53,490 --> 00:02:54,360 And here it is. 34 00:02:54,720 --> 00:02:56,340 Let's open it with Notepad. 35 00:02:59,800 --> 00:03:06,730 Here is the common intermediate language code that's now copied and pasted on this web page. 36 00:03:09,670 --> 00:03:15,760 Chopped up allows us to compile the common intermediate language code to see towards C-sharp code, 37 00:03:15,760 --> 00:03:16,780 it translates to. 38 00:03:20,920 --> 00:03:21,880 And here it is. 39 00:03:22,240 --> 00:03:23,980 Let's fund the program class. 40 00:03:25,670 --> 00:03:28,520 And this is how it looked like originally. 41 00:03:29,640 --> 00:03:33,840 Well, the changes done by the compiler are not very exciting. 42 00:03:34,320 --> 00:03:41,190 We can see that the unused variable has been renamed to something shorter and that the strings have 43 00:03:41,190 --> 00:03:42,120 been learned. 44 00:03:42,630 --> 00:03:44,520 Now let's do the same thing. 45 00:03:44,670 --> 00:03:46,110 But in the release build. 46 00:03:53,790 --> 00:03:58,650 First of all, the result of compensation should end up in the release folder. 47 00:03:59,910 --> 00:04:00,570 Here it is. 48 00:04:03,180 --> 00:04:09,810 I will again drop the deal to elders and save the common intermediate language code and then paste it 49 00:04:09,960 --> 00:04:10,800 to our club. 50 00:04:13,230 --> 00:04:17,310 This time, the Houston variable has been removed completely. 51 00:04:18,060 --> 00:04:24,000 This is just an extremely simple example of what the compiler can optimize in the release mode. 52 00:04:24,300 --> 00:04:28,620 But be aware that those optimizations can be much more serious. 53 00:04:28,980 --> 00:04:31,590 The compiler can simplify or in-line. 54 00:04:31,600 --> 00:04:39,400 The method calls remove whole pieces of code that it doesn't find useful, etc. the algorithm behind 55 00:04:39,400 --> 00:04:44,340 it that is quite sophisticated and it depends on the compiler version as well. 56 00:04:44,340 --> 00:04:51,240 As such, low level details like how big the methods are, we can configure some of the optimization 57 00:04:51,240 --> 00:04:51,990 settings. 58 00:04:52,140 --> 00:04:58,530 For example, if we don't want some method to be in line, we can add the no inclining attribute to 59 00:04:58,530 --> 00:04:58,910 it. 60 00:04:59,390 --> 00:05:05,460 In the article attached to this lecture, I left a link to an article in which you can read more about 61 00:05:05,460 --> 00:05:05,970 that. 62 00:05:06,970 --> 00:05:13,270 The code optimization done by the compiler is the main difference between the debug and release modes. 63 00:05:13,660 --> 00:05:20,350 Please note that if we want some code to be executed only in one of those modes, we can put it in if 64 00:05:20,350 --> 00:05:24,640 debug or if release conditions are pre-processed or directives. 65 00:05:36,600 --> 00:05:42,220 Now this line will be executed if you run the application built in the realist mode. 66 00:05:42,540 --> 00:05:44,040 If you are in the debug mode. 67 00:05:44,130 --> 00:05:45,540 This line will be run. 68 00:05:47,930 --> 00:05:52,880 And here it is, makes sense because the realist mold was selected here. 69 00:05:54,110 --> 00:06:00,830 Let's summarize during the release build, the compiler applies optimizations it finds appropriate. 70 00:06:01,220 --> 00:06:06,110 Because of that, the result of the build is often smaller and it works faster. 71 00:06:06,500 --> 00:06:13,040 On the other hand, it's harder to debug because the compiled result doesn't match the source code exactly. 72 00:06:13,760 --> 00:06:14,940 During the interview. 73 00:06:14,960 --> 00:06:22,190 You can be asked How can we execute some piece of code only in the debug or only in the release mode 74 00:06:22,940 --> 00:06:25,280 by simply placing this code inside? 75 00:06:25,550 --> 00:06:33,530 If the bug or if this conditional preprocessor directives will actually talk more about them in the 76 00:06:33,530 --> 00:06:34,400 next lecture. 77 00:06:34,910 --> 00:06:37,480 Thanks for watching this one, and I'll see you there.