Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 195 196 [197] 198 199 ... 796

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

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2940 on: September 26, 2012, 02:38:53 am »

Hey Mego, thanks for the link to Project Euler, which is duly awesome.  Thanks to that I've finally realized how much this:

Code: [Select]
if (n % x == 0) and is_prime_number(x): pfactor = x
# time: <1 sec

is better than this:

Code: [Select]
if is_prime_number(x) and (n % x == 0): pfactor = x
# time: >5 sec
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2941 on: September 26, 2012, 03:04:55 am »

My school is offering an Information Tech class, which is computer app plus C++.

...Should I take it, or should I just go with a foreign language? :P I have a feeling it'll be just dabbling in programming.


Project Euler is too hard for me maths wise; I can't understand most of the problems there :c
Also, I don't see what's the difference between those two :-0
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2942 on: September 26, 2012, 03:06:26 am »

A bit of computer takes you further than a bit of foreign, career/utility wise.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2943 on: September 26, 2012, 03:09:31 am »

But (I think) I already know what they're going to teach. All of it. :<

Also, I don't believe in Korean public high schools' ability to teach both foreign languages and inf tech. :C
« Last Edit: September 26, 2012, 03:11:46 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2944 on: September 26, 2012, 03:54:44 am »

Also, I don't see what's the difference between those two :-0

Neither did I beforehand, not really.  The problem was to find the largest prime factor of n:
Spoiler (click to show/hide)
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2945 on: September 26, 2012, 03:59:34 am »

Oh, for and, if the first thing is wrong, the second is skipped?

Something new every day. o_O


...Does this work in C++ too? In which if( is_prime() && n%x == 0) is slower than if( n%x == 0) && is_prime()?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2946 on: September 26, 2012, 04:05:31 am »

Hey Mego, thanks for the link to Project Euler, which is duly awesome.  Thanks to that I've finally realized how much this:

Code: [Select]
if (n % x == 0) and is_prime_number(x): pfactor = x
# time: <1 sec

is better than this:

Code: [Select]
if is_prime_number(x) and (n % x == 0): pfactor = x
# time: >5 sec
Project Euler is too hard for me maths wise; I can't understand most of the problems there :c
Also, I don't see what's the difference between those two :-0

Most computer languages short-circuit "and" tests: they skip evaluating the right-hand-side completely if the left-hand-side is false. In this case, because the speed of the two sides is different, you can get a significant speed boost by putting the fast test (the (n % x == 0)) on the left, and putting the slow test (the is_prime_number(x)) on the right, where it will get skipped a lot of the time.

The skipping is often used with validity testing (e.g. c++ code):
if (pX != nullptr && pX->Y > 0)
...will only try to access Y in pX if pX is not null. This is important because trying to access anything via a null pointer will crash the program.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2947 on: September 26, 2012, 04:06:36 am »

"or" tests also short-circuit, but the opposite way: The right isn't evaluated if the left is *true*.

For the exact opposite of the test above:
if (pX == nullptr || pX->Y <= 0)
Will again not try to access pX->Y if pX is null, this time resulting in a true if instead of a false one.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2948 on: September 26, 2012, 04:11:42 am »

So, in short, you put the fastest test on the left side, with both "and" and "or". Also, if the tests are more or less the same, but you expect one to fail a lot more than the other, putting that one in front helps (in case of an and) or on the right (in case of an or).

So, for instance:
Code: [Select]
for x = -1 to 1000 {
  if (x < 10 && x > 0)
}
is faster than
Code: [Select]
  if (x > 0 && x < 10)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2949 on: September 26, 2012, 05:31:36 am »

Actually I just tried it using GCC Explorer and they both optimise to the same assembly.
Confusing assembly that I can't find the > 0 test in at all.

Code: [Select]
#include <iostream>

int main()
{
  int y;
  for (int x = -1; x < 1000; x++)
  {
    if (x > 0 && x < 10)
    {
      y = x;
    }
  }
 
  std::cout << y;
 
  return 0;
}

Code: [Select]
main:
mov eax, -1 // x = -1
.L3:
lea edx, [rax-1] // edx = x - 1
cmp edx, 8 // compare edx to 8
cmovbe esi, eax // if (edx <= 8) y = x
add eax, 1 // x++
cmp eax, 1000 // compare x to 1000
jne .L3 // jump to L3 (loop start) if x != 1000
sub rsp, 8 // from here on deals with the cout call
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
xor eax, eax
add rsp, 8
ret
sub rsp, 8
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
add rsp, 8
jmp __cxa_atexit

(note it still loops 1000 times even though there's no result from doing so apart from wasting time. Make sure you break loops as early as possible!)

EDIT: Figured it and it's really fecking clever. cmovbe is a conditional move based on an *unsigned* below-or-equal comparison. When x-1 is negative, it'll interpret it as a very large number, and therefore > 8 (>=10 in the original code). The signed version is cmovle (conditional-move-on-less-or-equal). Yes that's confusing.
« Last Edit: September 26, 2012, 06:05:19 am by Thief^ »
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2950 on: September 26, 2012, 08:13:42 am »

Actually I just tried it using GCC Explorer and they both optimise to the same assembly.
Confusing assembly that I can't find the > 0 test in at all.

Code: [Select]
#include <iostream>

int main()
{
  int y;
  for (int x = -1; x < 1000; x++)
  {
    if (x > 0 && x < 10)
    {
      y = x;
    }
  }
 
  std::cout << y;
 
  return 0;
}

Code: [Select]
main:
mov eax, -1 // x = -1
.L3:
lea edx, [rax-1] // edx = x - 1
cmp edx, 8 // compare edx to 8
cmovbe esi, eax // if (edx <= 8) y = x
add eax, 1 // x++
cmp eax, 1000 // compare x to 1000
jne .L3 // jump to L3 (loop start) if x != 1000
sub rsp, 8 // from here on deals with the cout call
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
xor eax, eax
add rsp, 8
ret
sub rsp, 8
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
add rsp, 8
jmp __cxa_atexit

(note it still loops 1000 times even though there's no result from doing so apart from wasting time. Make sure you break loops as early as possible!)

EDIT: Figured it and it's really fecking clever. cmovbe is a conditional move based on an *unsigned* below-or-equal comparison. When x-1 is negative, it'll interpret it as a very large number, and therefore > 8 (>=10 in the original code). The signed version is cmovle (conditional-move-on-less-or-equal). Yes that's confusing.

Different compilers (and even compiler versions!) will optimize differently, especially if you're using optimization flags. Because of that, it's best to write code in the most optimized form you can. Don't take a smart compiler for granted.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2951 on: September 26, 2012, 09:42:07 am »

Also, I don't see what's the difference between those two :-0

Neither did I beforehand, not really.  The problem was to find the largest prime factor of n:
Spoiler (click to show/hide)

Here's a much faster algorithm:
Spoiler (click to show/hide)
Logged

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2952 on: September 26, 2012, 09:52:28 am »

Different compilers (and even compiler versions!) will optimize differently, especially if you're using optimization flags. Because of that, it's best to write code in the most optimized form you can. Don't take a smart compiler for granted.

Oh of course, but you also shouldn't try to optimise something until you've proven that there's a problem with it (and afterwards, can prove that you helped). Optimisation's not as easy as it sounds.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2953 on: September 26, 2012, 10:14:09 pm »

Different compilers (and even compiler versions!) will optimize differently, especially if you're using optimization flags. Because of that, it's best to write code in the most optimized form you can. Don't take a smart compiler for granted.

Oh of course, but you also shouldn't try to optimise something until you've proven that there's a problem with it (and afterwards, can prove that you helped). Optimisation's not as easy as it sounds.

Yes, I'm aware of this. Working product before shiny product.

On another note, Java is baffling me.

Spoiler: GameButton.java (click to show/hide)
Spoiler: GameGUI.java (click to show/hide)
Spoiler: GamePieces.java (click to show/hide)
Spoiler: GUITest.java (click to show/hide)

Everything appears to work fine. The window appears with a button in it, which has the icon represented by black.png. Except, when I try to click it, nothing happens. The various debugging things I put in GameButton.mouseClicked and GameGUI.processClick aren't doing anything. Where am I going wrong?

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2954 on: September 27, 2012, 04:06:27 am »

Here's a much faster algorithm:
Spoiler (click to show/hide)

Indeed it is, thanks.  Not just faster either:

Spoiler (click to show/hide)
Logged
Pages: 1 ... 195 196 [197] 198 199 ... 796