Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Counting set bits

Counting set bits

Scheduled Pinned Locked Moved C#
databasedata-structuresquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaleyX
    wrote on last edited by
    #1

    What would be the fastest way to count the number of set bits in a byte? I currently use a static array which has the number of bits set for each possible combination of set bits then use the byte value to index into that array. Are there faster ways to do it?

    P 1 Reply Last reply
    0
    • P PaleyX

      What would be the fastest way to count the number of set bits in a byte? I currently use a static array which has the number of bits set for each possible combination of set bits then use the byte value to index into that array. Are there faster ways to do it?

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      your technique is generally the fastest... have a look at http://forums.devshed.com/t70218/s.html?highlight=lookup+values[^] for a discussion....


      "When the only tool you have is a hammer, a sore thumb you will have."

      P 1 Reply Last reply
      0
      • P Philip Fitzsimons

        your technique is generally the fastest... have a look at http://forums.devshed.com/t70218/s.html?highlight=lookup+values[^] for a discussion....


        "When the only tool you have is a hammer, a sore thumb you will have."

        P Offline
        P Offline
        PaleyX
        wrote on last edited by
        #3

        Is it? Drat! It's where my code spends most of its time - although it is doing it billions of times so I don't suppose I can complain. :rolleyes: Thanks for the reply.

        P 1 Reply Last reply
        0
        • P PaleyX

          Is it? Drat! It's where my code spends most of its time - although it is doing it billions of times so I don't suppose I can complain. :rolleyes: Thanks for the reply.

          P Offline
          P Offline
          Philip Fitzsimons
          wrote on last edited by
          #4

          I have a general rule when it comes to optimization: - the fastest way to do something is not to do it i.e. does you code really need to count the bits? is there a way to avoid it? what are you counting bits for btw?


          "When the only tool you have is a hammer, a sore thumb you will have."

          P 1 Reply Last reply
          0
          • P Philip Fitzsimons

            I have a general rule when it comes to optimization: - the fastest way to do something is not to do it i.e. does you code really need to count the bits? is there a way to avoid it? what are you counting bits for btw?


            "When the only tool you have is a hammer, a sore thumb you will have."

            P Offline
            P Offline
            PaleyX
            wrote on last edited by
            #5

            It's a database application which stores the presence or non presence of variable values in bit vectors stored in byte arrays. Counts and analyses can then be run very quickly - for example: All people who live in England, drive a ford, eat out at restaurants once a week, read the Times and don't have a pet - on a database of 100 million individuals takes half a second. Counting the bits gives the total number matching the criteria after the logic processing has done its thang. TBH, it's not too much of a worry that the bit counting is taking a lot of the time as I am happy with half a second on 100 million individuals - and I doubt I will ever put 100 million individuals into a real database, I was just stress testing then seeing where it was spending its time.

            P 1 Reply Last reply
            0
            • P PaleyX

              It's a database application which stores the presence or non presence of variable values in bit vectors stored in byte arrays. Counts and analyses can then be run very quickly - for example: All people who live in England, drive a ford, eat out at restaurants once a week, read the Times and don't have a pet - on a database of 100 million individuals takes half a second. Counting the bits gives the total number matching the criteria after the logic processing has done its thang. TBH, it's not too much of a worry that the bit counting is taking a lot of the time as I am happy with half a second on 100 million individuals - and I doubt I will ever put 100 million individuals into a real database, I was just stress testing then seeing where it was spending its time.

              P Offline
              P Offline
              Philip Fitzsimons
              wrote on last edited by
              #6

              lol strange coincidence... :laugh: i used to design direct marketing systems for exactly this kind of thing (CMT/NDL/Claritas) check you IO and Page faulting...


              "When the only tool you have is a hammer, a sore thumb you will have."

              P 1 Reply Last reply
              0
              • P Philip Fitzsimons

                lol strange coincidence... :laugh: i used to design direct marketing systems for exactly this kind of thing (CMT/NDL/Claritas) check you IO and Page faulting...


                "When the only tool you have is a hammer, a sore thumb you will have."

                P Offline
                P Offline
                PaleyX
                wrote on last edited by
                #7

                Really? I used to write the software at Dudley Jenkins for their direct marketing "lifestyle" database. I have set up on my own now. I have got the IO and Page Faulting sorted, I believe. There is certainly very little page faulting when analysing the 100 million and the IO seems to have very little impact. It's been quite a ride getting this thing to work this fast in C# after writing similar software in C++.

                S 1 Reply Last reply
                0
                • P PaleyX

                  Really? I used to write the software at Dudley Jenkins for their direct marketing "lifestyle" database. I have set up on my own now. I have got the IO and Page Faulting sorted, I believe. There is certainly very little page faulting when analysing the 100 million and the IO seems to have very little impact. It's been quite a ride getting this thing to work this fast in C# after writing similar software in C++.

                  S Offline
                  S Offline
                  Sebastian Schneider
                  wrote on last edited by
                  #8

                  Speed is always difficult do maintain with so many different layers inserted between processor and application. C# feels good, though. Most of the times ;) Cheers Sid

                  P 1 Reply Last reply
                  0
                  • S Sebastian Schneider

                    Speed is always difficult do maintain with so many different layers inserted between processor and application. C# feels good, though. Most of the times ;) Cheers Sid

                    P Offline
                    P Offline
                    PaleyX
                    wrote on last edited by
                    #9

                    True, I rather like C# now, I wqasn't to keen at first and I still have to do stuff I would rather not just to get the performance but then that's often the case in most languages - at least C# remains reasonably readable.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups