Programming Thread

Started by the-pi-guy, Mar 13, 2016, 10:39 PM

previous topic - next topic

0 Members and 4 Guests are viewing this topic.

Go Down

Legend

Do you ever run a program, and then it runs way faster or better than you expected, and you're totally convinced something must be up?

I wrote a program to check 500 billion combinations of numbers , and it finished in like 5 minutes.  That didn't seem so bad.
Every day!

But normally that's because something actually is messed up.

the-pi-guy

So I have 5 graphics books (each that I got free/super cheap), and at least 2 other books that talk about graphics, but aren't their main subject.

It is kind of interesting looking over what topics each book covers.  

There's a lot of standard material like viewing, transformations, and lighting.  

But some of the books go much more in depth with a subject than the others.  And some seem to cover things the others don't at all.  Which is pretty cool.  
Like one of these books dedicates a full 20 page chapter to AA, whereas most of the other books only have about a page on it.  
One of these also has a chapter on fluid simulation and another on rotational physics.  

A couple of these books also dedicate a lot of space to things like fractals and ray tracing.  

One of them spends like 8 pages on hidden surface removal, and another spends like 30.  


I really want to get into all the material in these books.  Going to be a lot, I don't know when i'll have time.  

It's pretty cool, nonetheless.  

the-pi-guy

Quote
Twitch plays visual studio. That ought to be good.
https://www.reddit.com/r/AskReddit/comments/9h45ae/comment/e69jxyf

I've thought about something like this before.

It'd be awesome to make something that actually works this way.  

Legend

https://www.reddit.com/r/AskReddit/comments/9h45ae/comment/e69jxyf

I've thought about something like this before.

It'd be awesome to make something that actually works this way.  
How would you get it to even produce viable code?

the-pi-guy

How would you get it to even produce viable code?
Honestly, I don't even think it could.  
Not that it's impossible, but it's close enough...  

But that's part of why it'd be cool to get something like that to actually work.

Legend

I am so tired and I should not be programing. I was super confused debugging something since the output seemed impossible, but then I realised I had written Debug.Log instead of Debug.LogError on one line which was causing me to miss those reports. It had me pulling my hair out trying to figure out how the error didn't exist until the later section.

Then a couple minutes ago I was just using a Vector2 to calculate an angle instead of a Vector3, and was weirded out why I kept getting seemingly impossible results.

the-pi-guy

Oct 09, 2018, 02:40 PM Last Edit: Oct 09, 2018, 02:56 PM by the-pi-guy
It's fun to figure out why things are going so slow. 

So I am making a program to calculate the partition function. 

I made an oversight while building it.

So basically it's a recursive function that depends on the results of smaller values.  So I figured a way to make it fast, would be to save the value in an array.  It basically checks if the value is 0, and then if it isn't it'll return the value.  This is nice, as the array starts off with all 0's, so any value that hasn't been calculated should be 0.

A problem here is that since I'm doing this function (mod 11), any function is likely to be 0 quite a bit of the time.  And it looks like this function is 0 (mod 11) a large portion of the time.  So the function sees that f(N) is apparently still 0 (figuring that it hasn't been calculated), and it decides to calculate it, returning 0. 

So I fixed it, by giving the array a number it can't have.  So a -1 means it hasn't been calculated yet, and a 0 is a 0. 

Way faster. 

Just to have a picture of how much faster:

In the improved function,
F(100) looks something like this:
 F(100) =F(99) + F(98) - F(95)-F(93)+F(88) + F(85)-F(78)-F(74)+F(65)+F(60)-F(49)-F(44)+F(33)+F(27)-F(14)-F(7)

For F(100), it should be 16 additions.

Instead, for the old function:
It would recalculate most of those, so for F(100), it was closer to 188 additions instead of 16. 
And actually it gets so, so, so, so much worse than that, because it would still have to recalculate all the values for F(99) in a similar fashion. 
And it would have to recalculate F(98) for both F(99) and F(100).  That might be another ~380 additions.
And it would have to recalculate F(95) for F(100), F(97), and F(96).  That might be another ~570 additions. 

We're likely at around 1000 additions to calculate 3 numbers, F(100), F(98), F(95), and that is basically assuming that we don't calculate any of the smaller numbers. 


So that should give you a picture of how stupidly, monstrously inefficient this one small error made for.

And if it does not.  I decided to put everything back, add in a variable to count how many additions it took to calculate F(100) and only F(100).

And it is a fricken monster of a number. 

Spoiler for Hidden:
<br>F(100) took 271196898 additions<br>


Like wtf. 


The new function gives me this output for 100:  (assuming all the previous values are calculated)
Spoiler for Hidden:
<br>F(100) took 16 additions<br>

Assuming none of the values are previously calculated, it comes to 1054 additions. 
Spoiler for Hidden:
<br>This program literally runs 16,949,806.13 times faster for F(100).&nbsp; <br>



So yeah, I'm feeling pretty giddy right now.

Legend

I love when optimizations work out like that.

It's also amusing how the inefficiencies can sneak up on you. F(50) might be a blip on your computer so you assume it's all fine so then you try F(100) and get stuck waiting.

the-pi-guy

I love when optimizations work out like that.

I'm so giddy, this just feels insane.  
That's just an insane improvement.  

It's also amusing how the inefficiencies can sneak up on you. F(50) might be a blip on your computer so you assume it's all fine so then you try F(100) and get stuck waiting.
And that's exactly what happened.  
I was just doing 1 - 200, and the first 25 were almost instant.  And the next group was pretty fast.  But then I was waiting like 20 minutes, still wasn't done and the new values were taking a while to show up.  

That's when I realized something was up.  

Legend

Unity has been so annoying as of late. Half the time I feel like I'm having to fight it. If I could just go into the source code and make a change things would be trivial, but no. Instead I have to make inefficient workarounds that are laughably stupid.

the-pi-guy

Oct 11, 2018, 02:29 AM Last Edit: Oct 11, 2018, 02:44 AM by the-pi-guy
So a while ago, I made up a matching game for Android for Japanese words. 
I kind of gave up on it.  There was a weird blue screen that im not sure where it comes from and loading the words takes way too long.

 
I want to go back and fix it up though. 
And today, I finally did it.
 
Took about an hour to fix up the vocab importer. 
Got rid of all the inefficiencies.  Rebuilt the entire vocab list so the vocab importer could just take every word and save it.  I also strangely found a major error that somehow went unnoticed.  In my character importer when converting the unicode, I have a hexadecimal converter.  The issue is that the converter was built to manage 10=A, 11=B, 12=C, etc.  But the character list had a mix of lower case and upper case letters (not sure why I would have done that.)  So any characters that had a lower case in their encoding, did not get evaluated correctly. 

Instead of having to match every single character.  I knew this was super inefficient, but it had a good benefit at the time.  But the benefit doesn't outweigh the gigantic loading times.  Basically it would be on the order of 180,000,000 matches.  (Assuming 9000 vocab words * 2500 characters to match * 8 (made up average) average word length)   

So yeah.  That many matches on the computer is easy, but for a phone, it's ludicrous. 
Now it doesn't do any matching. 


To do:
-keyboard access for typing game (maybe just having a user text box would automatically open it.  Gotta figure that out)
-fix intro blue screen.  I still don't know where it comes from.  I don't know enough about Android software.  But basically when I launch a game, it starts off with a blue screen that disappears when I touch the screen.  My game still has a blue background.
-make it prettier
-also figure out some small issues, with some words being incorrectly marked incorrect.

the-pi-guy

Oct 11, 2018, 04:15 PM Last Edit: Oct 11, 2018, 04:53 PM by the-pi-guy
Trying to install Android Studio on my laptop.
It's working but for some reason a bunch of things are missing. 
There's no android under the tools option. 
The emulator isn't there.

And after a good hour of trying to fix it, there's hardly anything online that has helped either problem. 

Finally got it.

the-pi-guy

Oct 13, 2018, 01:49 PM Last Edit: Oct 13, 2018, 01:50 PM by the-pi-guy
.
It's like really, really good that my math function got fixed.  
Have to solve for the first ~250,000,000 numbers.  It takes a long time.  
Probably took about 10 hours.  
Not exactly sure.  I have it count how long, but the computer was also sleeping for a lot of the time.  (I have it running on my laptop)

Took about 20 hours overall.  It must have finished the first part just a few hours ago.  

Now I have a 600 MB text file with just numbers.

Soon I'll have 2.

Legend

It's like really, really good that my math function got fixed.  
Have to solve for the first ~250,000,000 numbers.  It takes a long time.  
Probably took about 10 hours.  
Not exactly sure.  I have it count how long, but the computer was also sleeping for a lot of the time.  (I have it running on my laptop)

Took about 20 hours overall.  It must have finished the first part just a few hours ago.  

Now I have a 600 MB text file with just numbers.

Soon I'll have 2.
That's really nice. If it was ever so slightly slower, you'd feel like you did something wrong.

How did you handle memory for that? An array with 250,000,000 entries feels like it could have issues if just used willy nilly.

the-pi-guy

Oct 13, 2018, 02:57 PM Last Edit: Oct 13, 2018, 03:24 PM by the-pi-guy
That's really nice. If it was ever so slightly slower, you'd feel like you did something wrong.
I actually sat down and did the math to see about how long it should take, assuming everything was working properly. 

Was well in line with what was happening. 

How did you handle memory for that? An array with 250,000,000 entries feels like it could have issues if just used willy nilly.
I just used an array. 

250,000,000 ints requires about 1 GB of space. 

(It's very good to have 16 GB of RAM).

I basically had a function like
Code: [Select]

f(N){
for( i=0; i<=N; i++)
array[i] = calculate(i);
}

Where calculate just calculates the number. 

All the previous calculations have to be accessible for the calculate function.

Go Up