10X Your Code with ChatGPT: How to Use it Effectively

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get chat gbt to write some code for us [Music] well that was colorful and easy but does it work does it run it does it provide the right answers we'll find out today in Dave's Garage as we learned that and a lot more about coding with chat gbt [Music] hey I'm Dave welcome to my shop today I'm going to show you how to significantly update and improve your coding skills by incorporating chat GPT into your daily workflow and I believe that it's true whether you're just starting out or if you're an old gray bearded wizard it's an important tool that you won't soon work without I've been coding for a living since the late 1980s having been an operating systems engineer at Microsoft on MS-DOS and every version of Windows on through XP and despite some widespread cynicism about its accuracy and value by those who perhaps don't know how to make use of it properly I can tell you with great certainty that what I'm going to show you today is something that every relevant encoder will be doing not long from now I believe it's the biggest inflection points in programming since the web itself now programmers have been cheating on their homework since time immemorial copying from textbooks and their friends code listings and onto most recently finding answers to their problems on sites like stack overflow with careful searching the odds are you'll be able to find a code snippet that does something like what you're looking for and hopefully is close enough to give you the inspiration that you need to then write the code on your own with chat GPT we can just come straight out and ask it what we're looking for and it will produce code but there's a lot more to using chat gbt effectively than that and those are the skills that we'll learn today but here's the thing so far as I can tell chat GPT has a great deal more knowledge than intelligence chat GPT is a little bit like an eight-year-old hyper smart kid that has read the whole internet and then they sat down and read all the code on the internet and then they thought real hard about it for a long time but it's still an eight-year-old slightly odd kid and maybe like me that kid's got some autism going on because he or she is going to take your requests very literally and they'll do better if you're forthright with them than if you make them assume things for you and so it is with Chachi EBT as well to get started let's look at some Basics no pun intended before we get on to some of the subtleties that make using it powerful enough to be professionally practical like most tools when it doesn't work right you should blame the operator first and so before we can craft something really elegant we've got to learn how to rip some particle board as it were so let's first solve some simple coding problems using chat gbt back when I was still a bit like that slightly odd kid that I just described my own 1541 floppy Drive melted down and so for a few months all I had was the audio cassette to save things on as a result I was always looking for really simple things that I could solve with my Commodore 64. and one example I remember well was sitting down and writing a conversion utility to go to and from Roman numerals so let's see if chat GPT is as smart as I was back then by having it do the decimal to Roman numeral conversion now I speak python with a c accent at least so I'm always curious to see how other people do things let's kick things off then with a python example before moving on to the more muscular languages our first question then will be to ask it to write a complete python application that inputs a decimal number and displays it in Roman numerals I'll grab The Code by clicking copy code and then we'll paste it into Visual Studio code or we can get a better look at it first the code declares a map of Roman numeral symbols to their values such as m is 1000 C is 100 L is 50 and so on and even though I'm a C plus plus guy this actually demonstrates the power of python rather nicely in how the r and num are both enumerated as you go through the map what it does it counts how many of the symbols value such as 1000 for M can be wholly divisible in your original decimal number it then appends that many of the symbol onto the result basically by going in a greedy First fashion and taking out the largest value symbols first what it does it just measures how many of the symbols values would fit into your decimal number it then backs that money out then prints that many copies of that symbol down here is the main application definition I'm not used to python so this whole name equals main is a little Voodoo to me but I'm presuming all it does is ask for the decimal number as an input string converts it to int it then passes your decimal number to the decimal to Roman function which Returns the result and is printed out on the console let's run the app and put it through its paces first number I'll do is 38.88 which I believe is the longest one you can get under four thousand I'll try one two three four that looks correct five four three two and that is also correct if you accept the five M's in Louisville M with a bar so clearly it's able to crank out computer science work of the high school level but bear with me because by the time we have it saving memory on Parallel threads on both the CPU and the GPU I promise you you'll be at least a little bit impressed before we go there however I should note that you're not just limited to contemporary languages what if I ask you to write this program for the Commodore 64 using CBM basic V2 for example well his first answer is a bit sketchy is they included read and data statements on the command line to set things up and so I asked for it to all be programmatic instead that makes it a bit more verbose but also more straightforward to test the code I'll type it into the trusty old Commodore 64 and we'll see what we get chat gbt is a little rusty and its first attempts would not work for example it called a variable value which conflicts with the built-in command Val and that's the first clue of how to work with chat GPT when it makes a bad assumption immediately correct it it will usually remember the context for at least the remainder of your chat GPT session and incorporate it into all your future results in that session I told it a few criteria to enforce like doing everything in lower case using only two letter variable names and that the if then Clauses had to be on a single line it got close but it was emitting lines longer than 80 characters which is all basic and choked down so then I asked it to keep the lines under 80 wide and soon enough everything worked perfectly let's have a look let's see if I can still type on one of these and if chat gbt has done its job we can just type run and be prompted for the decimal one two three four it's a little slower but that is the right answer 388 similarly it's the right answer and so there you have it a successful attempt by chat GPT at writing code and languages from two completely different eras the Retro basic took a little more guidance to get right but the python ran exactly as it was produced but it won't always be so easy trust me now I hope at least about 85 percent of you are now thinking well that's great Dave but I don't have to code decimal to Roman conversion very often I write real code for a living well let's move on to C plus plus then we'll first replicate the python of basic results and then move on to some more high performance problems we'll ask for the same thing but this time in C plus plus and ask it to be compact so it all fits on the screen ideally foreign copy code and copy the code and paste it into Visual Studio code or we can get a better look at it here we see it finds 13 strings that represent the Roman numeral symbols and the 13 values that accompany those symbols mapped one to one it's pretty easy to look up one from the other using the same index in this case it's a little different than the python in that it doesn't proceed from its current place in the loop until the number has exhausted the amount of times that its value can be removed for the symbol so if the number is over 2000 it will have to remove M twice by backing it with the value one thousand from num and adding the symbol for m to the result and at the end it Returns the result string I would say that the use of C plus plus strings here really cleans up this code relative to what it would be if you were using character buffers and the program itself simply asked for then reads in the decimal number before passing it to decimal to Roma and then passing the result to the output stream in other words printing it on the terminal and after pressing compile I'll simply run it from the console it'll put in our favorite numbers one two three four remain again 388 for the long one run it again and we'll do the year 2023 and it is also correct let's go from high school to college by building a prime sieve a year or two ago we kicked off The Language drag racing series which has grown to more than 80 languages with more than 100 solutions from a to Zig and everything in between at its core was the original C plus plus version that I'd written and I want to see if chat gbt can craft something close enough to be at least competitive and so let's try something a little more complicated if not more realistic and then we'll look at how to really get value out of it let's have a crank go to Prime sib for the first 100 million integers and see how it does there then I'll up the ante much further my query for chat GPT will be more specific this time write a very compact but efficient Prime save of the numbers up to 100 million in C time the execution from allocation to freeing of the sieve report the elapsed time and the number of primes that it found now just as we've done before I'll submit the query to chat GPT and we'll get the code that it produces [Music] foreign we'll take that code over to visual studio where it's easier to see and have a look now it's not super efficient it goes well past the square root and up to the max where it doesn't need to and so on and so forth but it looks essentially correct so let's compile it run it and see if it produces the right results and in how much time we'll simply run the compiled executable and see how long it took to execute based on its internal timer and if it produces the right number of results which at 78 498 it does and it took five one thousandths of a second to do it for comparison we'll time the version I wrote myself before seeing that one and we'll see how it Compares for time my version executes in about one thousandth of a second so about five times faster than shot GPT and it's also following some more stringent criteria but it's a good start for chat gbt let's see how far we can take it well I set it up the ante so let's start to do that let's make it at least multi-threaded and not just multi-threaded but entirely cross-platform too so that it doesn't rely on any system API calls to do it my query for a sieve that would match up against the GitHub private project was complicated but roughly matched the rules it describes how the program should work and the essentials are that it's going to compute the primes up to 1 million on all the cores as many times as it can in five seconds it can only use one bit of storage per Civ element so one million bits per sieve it has to allocate and tear it on the memory Anew for each Civ pass and it cannot incorporate any advanced knowledge of other prime numbers other than one and two chat GPT wasn't able to produce a working solution right out of the box it kept getting the stats wrong or printing the total number of primes found on all civs rather than perceive and so on there were also bugs that I had to guide it to fix it's this kind of iteration refinement that is the key to using chat gbt first I'll show you the code we wound up with and then I'll take you through the process of how I got there starting in the apps main section we see that it saves away the current time so that it knows when five seconds are up it then pulls the maximum number of threads from the standard libraries Hardware concurrency value then for each of those CPU cores it spins up a thread which runs through the Run Civ call that we'll look at briefly it then sleeps for five seconds to let all the background threads do their work on the sieves until setting the stop sieving value to true so that all the threads will then exit to join make sure that all the threads are collected and have in fact completed before it proceeds it then runs one final single sieve on its own in order to count the number of primes that are found in any one pass before producing all the output statistics now over the top of the file the primesiv class is declared and it's largely the same as the single threaded Prime sieve that we saw in the last example so we'll just bypass it for now the Run Civ function which is the entry point for all of the background threads is where the magic happens it sets a timer it creates a copy of the sieve and it runs the sieve as soon as its prime instance is done it increases the local count of sieves performed as well as adding to the local time taken this code will keep spinning in a loop creating and running Prime sivs keeping track of them and their time until the stop saving value has been set by the main function when it has it returns its count in total time so in summary then it does precisely what we want the main function spins off one thread per core and each of those core spins creating and running Prime sivs as fast as it can up to 1 million until five seconds have elapsed when they get flagged to stop I tested and verified that the code worked and found that on my Mac Ultra Studio was able to perform 10 882 passes in those five seconds now impressive as that sounds the hand-coded version that I did for the prize project completed 87 000 passes in the same time let's have a look at some of the back and forth iteration that I had to do with chat GPT to get it to produce a working solution that met the requirements after all it is this refinement in addition to in the correction of the context that is important it's essentially how to iterate on a solution with chat gbt as your partner in development it will do the heavy lifting and the boring Parts while you land the learner die towards its progress now before I hold this example up as some kind of proof that it can now do your day job you might be even more clever yet after all what fun is just writing a prime step that uses a CPU let's invite the GPU to the party and see if chat GPT can actually gainfully employ it what should we ask for well how about write a multi-threaded prime step that employs the Cuda API to make use of the GPU wherever it could be expected to improve ultimate performance including stream compaction now the Gory details of Cuda programming are a little beyond the scope of what I can cover here today so I'll Breeze through the process of generating testing the code and instead focus on the back and forth refinement of the code as we go first I left it fairly open-ended I just set a multi-threaded C plus plus Prime save that makes use of the Cuda API to Leverage The GPU for faster SIV performance including stream compaction the sieve kernel is the important part where it scans for and then blocks out numbers that it finds to be prime which it does on multiple threads on the GPU at once jumping down to my next iteration I ask it straight out is there any way to make this sit faster and it offers multiple Solutions such as load balancing optimizing the block size using shared memory and so on and so I'm going to ask it to recreate the sieve using some of those methods and so I'm going to ask you for one that is segmented adds load balancing and segments to see specifically for a size of 100 million because why not let's use all the memory we've got here we find the code again for saving the kernel and for compacting the kernel now I'm not much of a Cuda programmer but I can already tell one problem here for sure it's doing Kuda Malik to allocate memory on the GPU and then it's touching it in the for loop from the CPU and that's not going to work unless you're using shared memory so that's going to be a change we're going to have to make next I ask it a series of questions like what the file name extension should be should be dot CU for Cuda file and in the actual state of the sieve is it using zero or one to represent inclusion or exclusion because I wasn't sure I went around and around with this memory access issue for a while before asking it to Simply do it so that the CPU makes the allocation and then hands everything off to a Cuda function that does the actual work next I ask it to explain to me it's stream compaction approach and then I suggest to it another approach that it could use for Cuda device synchronize and GPU memory and after some more iterations back and forth I finally tell it to please use unified memory on the GPU instead and without getting too far into the weeds of Cuda programming we can now see that it does a Cuda milk managed to get shared memory between the GPU and CPU the CPU then sets up the initial State then hands it off to Prime SIV kernel to do the actual work before finally synchronizing the device again finally I ask it to correct one problem where it's got a loop variable that's not properly declared and from there we get the code that It ultimately produces for us I hope the creation of a prime sieve running accelerated on the GPU on multiple threads is enough to convince you that with the requisite amount of hand holding chat GPT can be an essential tool in not just learning new Concepts but also in generating the fundamental framework behind whatever your project demands soon enough I'll be taking a look at how to go the other direction feeding your previously handcrafted code into chat GPT to have it find and fix bugs and defects in your code and logic make sure you're subscribed to this channel so that you don't miss it if you have any interest in matters related to autism Asperger's or ASD please check out the free book sample on Amazon for secrets of the autistic millionaire it's everything I know now about autism and Asperger's that I wish I'd known back then and it's not just for people who are or believe they might be on the Spectrum but also for anyone who lives with loves or works with someone who might be thanks for joining me out here in the shop today remember I'm mostly in this for the subs and likes so please be sure to leave me one of each before you go today in the meantime and in between time I hope to see you next time right here in Dave's Garage
Info
Channel: Dave's Garage
Views: 168,918
Rating: undefined out of 5
Keywords: chat gpt, open ai, chatgpt tutorial, chatgpt examples, chatgpt explained, artificial intelligence, coding, software, code, programming, code samples, how to use chatgpt, coding with chatgpt, how to use chat gpt, chat gpt tutorial, how to, chat gpt examples, what is chatgpt, open ai chat gpt
Id: pspsSn_nGzo
Channel Id: undefined
Length: 17min 14sec (1034 seconds)
Published: Thu Feb 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.