 Now it's one thing to be able to write a shell script or a program to get what you need done It's another thing to write one efficiently and get things done quickly without using system resources or anything like that and doing Things elegantly and that's what this video is going to be about here. I actually have two scripts here I wrote both of these the one on the left I'll go ahead and say is a newer version of the script which is much much better than the one on the right Which I wrote several months ago flippantly and it is it has a very poor design now now neither of these scripts are particularly long The new one is 34 lines. The old one is 42 lines But I want to show you the performance difference that that can make so I'm gonna run the old script here And you'll notice that it takes it takes a second or so it takes a good bit of time for such a short script That's a little surprising. I mean it's not the end of the world just running the script But let's compare it to the new one and you'll see that it basically completes immediately I mean I really my finger isn't even off the inner key by the time that it's already finished In fact, we can use if you want to compare these we can run time on them And we'll see that this old one it takes about one and a half seconds That's that's a good bit of time for a small dash script actually While this one I mean it's nearly instantaneous. It's a couple milliseconds. It's nothing I mean, it's a totally negligible negligible amount of time. So I could run this script, you know Dozens a hundred something times or excuse me. I could run this smaller script, you know Gajillion times before this script ever finished now you might say, okay. Well the difference isn't that big What's an extra second or something like that? And well, that's the wrong mindset to have especially if you you want to create scripts for public consumption Because when you think about your system It is just a bunch of scripts and programs and how you design them efficiency differences like this can make a huge difference So in this video, I'm going to talk about Why what the difference between these two scripts are why this one is so much slower than this one? So at long story short it all amounts to you got to use streams and use streams effectively But before I talk about the the engineering behind them, let me just tell you what these scripts do now They do the same thing and I've actually done a video on this before I have a little file on my computer called folders and it is really just it has pairs of Directories with shortcuts corresponding to them. So how this works is You know, let me pull up bash What I have this script do or either of these scripts do is it reads a file like this And it creates shortcuts for bash Ranger and cute browser based on these. So for example, if I'm in bash and I press You know L and type that in that actually goes to my La Tech folder or if I press a that goes to my Articles folder, etc. etc. Or if I want to go to my art wall papers, I press excuse me I just press w a and that goes there and it shows me the contents and stuff like that So it the this script just makes out aliases for that and they also work in a ranger So if I'm in ranger, I can press a gl to go to the La Tech folder or T Wc to or what is it twl to go to my landscape folder or something like that and create a tab So I just have different shortcuts and these scripts generate them Now anyway, now that you sort of understand what they're doing Well, I'll talk about what the engineering difference between these scripts are now. Let's look at the bad one first, okay? So I'll ignore most of the stuff up here because it's mostly the same between the two scripts Just know that what this does the the specific way the file is outputting is that it creates separate files So for example, the one it creates for bash is shortcuts in my home directory and it creates all these aliases So that that's basically the output and what it's checking to do in these lines It's just checking to see if this file is properly sourced by my bash RC or something like that But let's actually get to the actual design principles behind the script. Now. Here's what it's doing computationally Now it has this script and you'll see here What it's going to do is it's going to go through each of these lines line by line and What it's going to do? Let's think about what it's actually doing for each of these lines So this for loop is going through and going to loop through all these lines And what it does is first off it sets the variable key Equal to really the first column. It's echoing line and awking, you know the first column. This is terribly I'm looking at this. It's like terribly written. This is so inefficient, but Well, it sets a variable key equal to this first column and it sets the the the second Or it sets deer equal to the second column. That's all that does And I'm already looking this like so embarrassed. I can't believe I ever even wrote this but I'll talk about why later So it does that it sets these variables and let's say it's on this first line It sets key equal to H and it sets a deer equal to you know that sequence of tilde plus slash Then what it does is it echoes out the proper syntax of the alias needed for bash With the key and with deer in it into the place. We've specified. We want our bash shortcuts Then it echoes those same things in the proper syntax for a ranger shortcut So here is my shortcut to go to a folder Here's my shortcut to make a tab in the folder to move a file to that folder and to yank a file to that folder And it also makes the binding for cube browser now The reason this is so in it you have to think of it in terms of what it's actually Outputting and we're and what this thing is in effect doing is for each of these lines that it's looping through It's not just setting variables which takes up time. It is outputting six times to different files It's outputting to the bash RC and then it's up Outputting to the ranger config four times and then the cube browser one time So this is an extremely inefficient script and in fact actually it goes through two files I mentioned I have a folder file here I have another file for configs and it's doing the same thing so for each of these lines It's looping through time and time and time and time again and outputting files time and time and time again And that's why it takes you know just to get through this document of 70 so lines It's taking you know a whole second or so to do this basic operation Now how does that compare to the better script? What is a better way to write do the exact same thing? So here is our our good script and in fact I'm sure that there are many people who are gonna watch this video and say oh there are even more ways You can improve on this, but let's go ahead and talk about what this is actually doing now Most of these lines are pretty much the same stuff and then we get down to again These are still sourcing the aliases, but the key lines here first off We just have less lines which you know can mean something all of this Corresponds to just these lines here now. What exactly is this doing now instead of Going through a loop and outputting to files time and time again. What I do here is just stream manipulation So let's actually let's actually run part of this script Manually so I'm going to go up to this I'm gonna copy that I'm gonna set folders equal to that so we can run this command And I am actually let's put it up here so you can see a little better So I'm gonna take this line And I'm gonna copy it out Well, maybe I should it doesn't matter if I So I'm just gonna Run for example the first part of this command now first We're gonna run said on the folders now said is just a stream editor Now if I run actually maybe I should run cat first So if we are just going to cat out this file to show the contents we see that okay Well, we have all this content all these contents here now if we run said this said command Just as an abbreviated this isn't so important But sometimes I put comments in my folder files this thing basically says Whenever you see a line that starts with a Pound sign just delete it just don't show it. That's all the said command does but ultimately This is just going to output the file without those comments. That's all that's gonna say now What we do here instead of iterating through each of these lines You know with a for loop or something like that what I actually do is just use awk to format this in a different way With awk we can take the elements of this file and just Reformat them in a different, you know just a different way and how we do that is with the print command So if I print this out I'm really just telling the file print out the following sequence print out alias and then the first column and then see Change directory and then the second column and then all the rest of the stuff that we need in a bash shortcut Now unlike our first actually I'm going to type this out for a second You can mull that over as I type this out because I don't I swear if I I can't even say anything while I'm typing all the stuff out. I actually think about it So oh wait, I'm stupid. I just copied it a second ago. Why don't I just do that see Now if I run this command just it what it's actually doing is instead of looping through all of that stuff and outputting It's really just modifying the stream that we have it's just taking this Sending it as a stream to awk and modifying the whole thing So instead of looping through all of this stuff and outputting one by one We've now just modified the entire stream. So this one command which happens basically Instantaneously, it's really just give me the contents of this file and perform this operation on the entire file It gives me all of that stuff that we had that for loop for Instantaneously, and then I just output it to the shell shortcuts folder now notice this I do the same thing I do it three times for each of the three configs. I have for bash ranger and cube browser Notice for the cube route. Well, we'll do the ranger one. That's the important one for the ranger one I'm gonna get rid of this so it outputs to the the shell I'll actually move it over here for the ranger one. I even have multiple lines. So I actually have new lines here So what I effectively have is I have the shortcut file and I can still Edit the stream such that I'm create for each one of the lines in this I'm actually gonna have four different lines in the ranger config Because I have four different shortcuts for going to the folder a new tab in the folder doing different operations to that folder But if I just run this you'll see that it's very easy to modify the stream To have multiple lines and again while previously We were outputting the file for each one of these lines. It is just one much more elegant line here Well, it's it's ugly looking, but it's very elegant Because we really just say Reformat for each of these lines reformat them as four different lines and print that whole file out And that's what it's doing. So this is the reason it's so much more effective because when you're thinking about files is being sort of When you're thinking about operations on the command line is being sort of streams I could cat out a file or I could effectively cat it out with said You know with this command which also deletes the comments I could treat that as a stream and then just give it to Auk and Auk is sort of filtering through it Performing this operation on it and it's just going directly to the place that you know We want it to end up at so this is a much more efficient way of writing than the for loop that goes through the whole thing and You know outputs the file a gazillion times And so that's more or less why it's a more efficient way of I guess doing things So anyway, I hope this has been I Guess instructive if you want to know anything more specific about the script. It's all my github. I guess I'll put a link to that Or you can ask any questions if you have suggestions to make this thing even faster Or more efficient or something like that. You can just say so Notice, you know, we have a good time here But it always it always helps to have you know more efficient code And there's probably something that I've totally overlooked here that someone's gonna point out But anyway, this has sort of been a perfect public service announcement because I think it is important to You know, I think writing scripts and writing programs even though I've only been you know writing scripts for my YouTube channel for the past couple years it's been a journey in You have to get in a mindset of how to do things efficiently and once you get in that mindset you there are huge Benefits to it and you're also doing honestly sometimes when people write people come to Writing programs or scripts with this mentality of oh, let me just import all these You know modules in Python and do all this complicated stuff that really a basic shell command a basic core utility That every Unix based operating system has can just do it instantaneously So anyway, it's just a public service announcement if you write something always make sure to make it more efficient So that's about it. So see you guys next time