Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 490 491 [492] 493 494 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 903644 times)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7365 on: May 12, 2015, 11:00:31 pm »

This was the linear version I stuck together earlier for testing:

Spoiler (click to show/hide)

This version has 1 addition and two copy operations per iteration. Modulus is a division, so it's slow. The ring versions, since it uses modulus, uses 3 divisions, two subtractions, one addition, and three pointer additions ("[ ]") per iteration. Gut feeling says this is slower, even if it's "less code".

I tested both with this framework:

Spoiler (click to show/hide)

The result on my PC was that the ring version took 56 seconds, and the "dumb" version took 20 seconds.
« Last Edit: May 12, 2015, 11:19:02 pm by Reelya »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7366 on: May 12, 2015, 11:18:19 pm »

Python, the "Duplo" of programming languages.

This is clearly a rational argument and I will accept it without explanation or evidence.
« Last Edit: May 12, 2015, 11:19:55 pm by Mephisto »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7367 on: May 12, 2015, 11:20:23 pm »

Duplo is large and blocky and easy to put together, and the results are colorful but clunky. Fits Python pretty well.

BTW there seems to be minor bugs in the ring version. Buffer should actually be like this:
Code: [Select]
int buffer[] = { 0, 1, 1 };otherwise it gives some wrong answers

I was thinking this can be massively sped-up. Get rid of the modulus, and thus the division-by-three. This can be achieved by first changing the ring to have period 4, then using a bitmask of the lowest two bits rather than a modulus.

This one is now four times faster than the modulus version, and faster than my original version:

Code: [Select]
long long fibonacci_ring(int n)
{
long long buffer[] = { 0, 1, 1, 2 };

for (int i = 4; i <= n; i++)
buffer[i & 3] = buffer[(i - 1) & 3] + buffer[(i - 2) & 3];

return buffer[n & 3];
}
« Last Edit: May 12, 2015, 11:34:19 pm by Reelya »
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7368 on: May 12, 2015, 11:34:51 pm »

BTW there seems to be minor bugs in the ring version. Buffer should actually be like this:
Code: [Select]
int buffer[] = { 0, 1, 1 };otherwise it gives some wrong answers
Depends which Fibonacci set you're using. There's a 0,1,1,2... one and a 1,1,2... one. I programmed the latter.

I tested both with this framework:

Spoiler (click to show/hide)

The result on my PC was that the ring version took 56 seconds, and the "dumb" version took 20 seconds.
Did a similar test using the <chrono> library with 100 runs of fibonacci(1000000) versus fibonacci_l(1000000) and got around 2959ms versus 538ms.
« Last Edit: May 12, 2015, 11:40:07 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7369 on: May 12, 2015, 11:44:14 pm »

Quote
Depends which Fibonacci set you're using. There's a 0,1,1,2... one and a 1,1,2... one.
you wrote "2,1,1" in your post though.

http://en.wikipedia.org/wiki/Fibonacci_number
And if you look at wikipedia, you can start at f(0) = 0 or at f(1) = 1, the sequence is the same after that for any value 'n'. If you're returning that f(0) = 1, then that's incorrect according to either starting point.

Also note, I got your reference code down much faster than before by using bitmasking rather than modulus, maybe you'd like to compare our new version vs the library version?

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7370 on: May 12, 2015, 11:55:29 pm »

Quote
Depends which Fibonacci set you're using. There's a 0,1,1,2... one and a 1,1,2... one.
you wrote "2,1,1" in your post though.

http://en.wikipedia.org/wiki/Fibonacci_number
And if you look at wikipedia, you can start at f(0) = 0 or at f(1) = 1, the sequence is the same after that for any value 'n'. If you're returning that f(0) = 1, then that's incorrect according to either starting point.

Also note, I got your reference code down much faster than before by using bitmasking rather than modulus, maybe you'd like to compare our new version vs the library version?
f(0) wasn't supposed to be called in my version. It was part of the invalid input I mentioned. Since 1%3==1, the buffer needed to start at index 1 to avoid using an (n-1).

Edit: Bitmask version runs in about 530ms. It's on par with the 'dumb' method, at least for my samples. Need to crank up the intensity.
The tests look something like this:
Code: [Select]
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
for (int j = 0; j < 100; j++)
{
fibonacci(10000000);
}
chrono::steady_clock::time_point end = chrono::steady_clock::now();

cout << "Time difference = " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << endl;

Results for 100 runs of f(10 million):
31481ms Modular
5336ms Dumb
5287ms Bitmask

There's some variance each time I run it (Firefox is definitely contributing,) but bitmask method seems consistently slightly better than dumb method.
« Last Edit: May 13, 2015, 12:26:30 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7371 on: May 12, 2015, 11:59:24 pm »

changing your original version to {0,1,1} and the start index to "3" will be correct for all values though, and you get f(0) correct as a bonus.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7372 on: May 13, 2015, 02:32:57 pm »

Why not just use the formula when you're computing really big numbers, it's probably O(1), assuming that powers are O(1).
Not entirely sure if you'll run into issues with rounding errors, though.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7373 on: May 13, 2015, 03:28:20 pm »

Not entirely sure if you'll run into issues with rounding errors, though.

It's likely that any rounding errors can be solved by rounding the result.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7374 on: May 13, 2015, 03:52:37 pm »

Just glancing at it, the rounding error gets bigger as n increases. Although the integer overflows long before it approaches anything that round() or equivalent can't solve.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7375 on: May 13, 2015, 05:25:14 pm »

Why not just use the formula when you're computing really big numbers, it's probably O(1), assuming that powers are O(1).
Not entirely sure if you'll run into issues with rounding errors, though.

Optimizing a given algorithm is a challenge / learning exercise.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7376 on: May 13, 2015, 05:40:23 pm »

Getting a compile bug.

Code: [Select]
C:\Users\~blahblahblah~\programs\slime mold simulator\main.cpp|14|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
Don't know what to do.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7377 on: May 13, 2015, 05:56:03 pm »

Quote
[enabled by default]
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7378 on: May 13, 2015, 06:03:16 pm »

Enable C++11 for your compiler. What IDE are you using?
Codeblocks. Specifically Codeblocks-13.12mingw. So it's using Mingw32-g++ for compiling, as far as I can tell. It was set to that when I first downloaded it.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7379 on: May 13, 2015, 08:35:52 pm »

you might have to enter it in yourself, as well. There'll be a text box there to do that.
Pages: 1 ... 490 491 [492] 493 494 ... 796