All right.
Are you excited to see how this goes?
Let's give it a try.
So in this next section, I create a string called pi which has within it the code to do this series
one minus the third plus a fifth, minus the seventh plus a ninth.
You can see that for yourself.
Check how many mistakes.
And then it's going to do that 100 a million times.
And really it's like 200 million times because it's you can see each of this.
It's doing like two two elements in that series.
So it's 200 million of this series.
And we'll see how close to pi it gets and then it will print the result.
So let's do that.
I'm now going to do some some evil code, which of course we can do because we know exactly what we're
doing.
But you should always be slightly suspicious anytime someone says exac.
ExAC, of course, is the command that runs Python code as specified in a string.
And now you can see with some confidence this string is doing nothing to to evil.
It's just doing this series.
But you should always double check any time someone gives you code that involves an exec to make sure
there's no funny business with your file system, or making an internet connection or anything like
that.
But in this case, you can be assured this is pretty safe and let's give it a try.
Exact pi.
Let's see what happens.
So there's a pause.
While Python thinks 200 million of those terms.
And there we go.
So let's see how well the Pi comes out after that.
3.14159265.
And that digit is wrong.
So 200 million of these terms don't get you that much.
Pi.
As you probably you may know, there are much better series that converge very quickly, but they are
a lot more code and a lot more gumph.
This is the benefit of being super simple to write, but slowly converging.
So it took 8.5764 seconds for that to run.
There's a if you run this a few times, you'll see that that changes a fair amount because of other
things going on on the machine.
And your machine is different to mine, but you get the general idea.
Okay.
So I should be able to run optimize GPT pi, and it should stream out C plus plus code that will do
the same thing by sending that string as part of the user prompt to GPT four.
Oh, let's see what we get.
Uh, this is what I mentioned to you.
It puts this at the top of the file, which we then strip out before saving this to disk.
This is the method.
Uh, not uh, you may or may not be a C plus plus guru, but, uh, regardless, from knowledge of similar
things, you can probably get a good handle on the fact that this is doing the same thing.
Uh, and it's doing a similar number of calculations, similar number of terms, and it has written
a file here, optimized C plus plus.
And there it is.
You can see it all looks good.
And it has included this package that needs to be included, which is because it uses a set precision
function.
And if I discovered if I don't put that in the user prompt, then it doesn't include this package which
causes it to break.
So there we go.
For whatever it's worth, we've given it the hint and it's done it.
Um, okay.
So look at this next cell.
So this next cell.
You remember, if you do an exclamation mark in Jupyter, then it, uh, it will um, uh, it will run
that as a terminal command.
So what I'm doing here is a terminal command.
Uh clang++.
Uh, and this particular statement is running optimized.
It's doing a compile and then running that C plus plus code.
Um, and later we're going to use an even more optimized way of doing this.
But this this is a good for now for your computer.
You may want to Google what it will take to to compile.
Um, if you're using a mac, then you may need to um, Uh, at least open Xcode to make sure you've
got the latest Xcode tools installed so that you have the latest versions of compilers.
Um, but but regardless, a quick Google will tell you how you compile a C plus plus file.
Um, once you've changed this line to be an optimized C plus plus compile of the file optimized CPP,
that is of course how you will run it.
So remember what we are looking to beat is 8.57 seconds.
Let's see how GPT four zero how its code has done.
Here we go.
And it's got the same answer I should imagine 658 at least to within.
Yes.
Looks like it.
I mean you'd expect some.
Oh no it's there's not even any rounding differences.
It looks like it's exactly right.
And it's in a fraction of the time 0.21.
Uh, so it is clearly, uh, um, somewhere between 10 and 100 times faster.
You'll find later, when we optimize further, we can do even better than this.
But.
But with these flags, that is what we get.
It's pretty impressive.
We've just built something that can do a conversion of code from Python to C plus plus and run it many
times faster.
Let's see how Claude does.
Well, let's just quickly compare the code.
The code looks pretty similar to me.
Uh, you will find, if you look at it, that there are some subtle differences.
For example, there is this stuff here, um, which is to do with, uh.
Well, I am not a C plus plus expert, although I did code in C plus plus some time ago, 1 or 2 years
ago.
Uh, but, uh, sadly, that is all swapped out in my mind.
Uh, and I would certainly never have got to this level of expertise.
It's putting in, I believe, various hints to suggest that things can be parallelized, but I sort
of suspect with some of this stuff the compiler would apply that anyway in both cases, but we will
see.
We'll see if this actually makes any real difference by running the same command for.
Uh, sorry, did I run that?
Let's try that again.
Here we go.
And this is the answer that we got.
Um, and it looks like, um, I think we may have to go back and do.
Oh, no.
Here we go.
Sorry.
This was Gpg's version, so GPT was 0.2121.
And here I've jumped ahead.
That's why I'm getting confused.
Sorry.
Let's go back up again.
Uh, I'm going to have to write this down 0.2131 is GPT version and Claude's Version is in 0.2121.
So Claude was a hair faster than GPT.
But I gotta tell you, this is within in error bars.
If you run this a few times, it will vary by at least that amount.
So basically they've both produced code which when running on the machine, uh, has run at the same
speed.
I would expect that the actual compiled machine code is the same in both cases.
Um, which is what you would expect for a fairly simple problem.
Uh, so both GPT four and Claude have crushed it with a well optimized solution to this, uh, Python
code to generate pi.
Uh, but, um, uh, although, as I'll say, GPT four did need a few more hints than Claude, but they
both got there after the break.
We're going to move to a harder problem, much harder code to see how they can optimize.
I will see you in the next video.