You are not logged in. Please login at www.codechef.com to post your questions!

×

bitwise & and modulus %2 operator speed comparison

0
1

i dont remember the source but i had read somewhere that bitwise & of n with 1 to check if its even or odd is faster than checking n%2 . but while solving the problem http://www.spoj.com/problems/TRGRID/ traversing the grid, i checked if n is odd or even and same for m. in that i found the version of code which had % operator ran in 0.00 and the one with bitwise & ran in 0.03 and 0.02 (i ran this code two times) rest all code was same. per test case i am checking only one time the two numbers about even and odd. Can someone tell which is faster and if possible, give reasons cheers

asked 04 Jul '15, 03:20

ankursmooth's gravatar image

3★ankursmooth
711310
accept rate: 14%


Such a small difference in performance is because of the SPOJ server. Try submitting the same solution twice and you may see a difference of 0.01-0.02 due to the load on the server. Plus, majority of the execution time of the code is because of input/looping, therefore using '&' operator though faster, is unable to provide a concrete speed up in this case.

link

answered 04 Jul '15, 08:20

anonxyz596's gravatar image

1★anonxyz596
1
accept rate: 0%

edited 04 Jul '15, 08:21

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×184
×15
×15

question asked: 04 Jul '15, 03:20

question was seen: 1,669 times

last updated: 04 Jul '15, 08:21