Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 99217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:20:43+00:00 2026-05-11T00:20:43+00:00

I’m interested in speed, not good looking code, that is why I’m using array

  • 0

I’m interested in speed, not good looking code, that is why I’m using array and not list(of integer).

I have an array looking like: 0,1,0,1,1,0,1,0,1,1,1,0,0,1

I’m interesting in the position of each number so I can later pick one randomly.

so what I do is looping through the array to take position number of each 1 then creating a new array looking like this: 2,4,5,7,9,10,11,14

is bitwise could be used here? I have no idea

code look like:

Private Function theThing() As Integer()     Dim x As Integer     'arIn() would be a parameter     Dim arIn() As Integer = {0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1}     Dim ar() As Integer = Nothing     Dim arCount As Integer = -1      For x = 1 To arIn.GetUpperBound(0)         If arIn(x) = 1 Then             arCount += 1         End If     Next      If arCount > -1 Then         'using redim preseve is slower than the loop above         ReDim ar(arCount)          arCount = 0         For x = 1 To arIn.GetUpperBound(0)             If arIn(x) = 1 Then                 ar(arCount) = x                 arCount += 1             End If         Next     End If      Return ar End Function 

* EDIT *

current solution(10% to 15% faster) is now

Private Function theThing() As Integer     Dim ar() As Integer = {0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1}     Dim arLenght As Integer = ar.GetUpperBound(0)     Dim arCount As Integer = 0     Dim x As Integer      For x = 1 To arLenght         If ar(x) = 1 Then             ar(arCount) = x             arCount += 1         End If     Next      dim r As New Random()      Return ar(r.Next(arCount)) End Function 

I don’t think it can be optimized more than that, unless someone find a way to do exactly what the solution does but way faster

Before this question, my whole thing was able to do about 25500 run each 10 seconds.

Now, it can do over 32250 all the time, a 21% increase, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:20:43+00:00Added an answer on May 11, 2026 at 12:20 am

    Few tips on the original algorithm:

    1. Try storing the results of arIn.GetUpperBound(0) in a variable. I don’t know how VB makes it’s loops, but there is a chance that the function gets called once every iteration. You should check that though.
    2. That If arCount > -1 is always going to be true. Remove it.

    If you wish to keep the same inputs/outputs, then I don’t think there is much else that can be improved.

    Now if you wanted a function that does the random selection too, then it might be a bit better. I’ll write in C# since I know it better. You should be able to understand:

    public static int GetRandomSetBit(int[] AllBits) {     // Perhaps check here if AllBits is null/empty. I'll skip that for now.      int L = AllBits.Length;     int SetBitCount = 0;      // No point to save a few bytes here. Also - you can make a global array     // large enough for all occasions and skip allocating it every time.     // In that case consider automatic resizing and watch out for     // multithreading issues (if you use several threads).     int[] GoodPositions = new int[L];      for ( int i = 0; i < L; i++ )         if ( AllBits[i] != 0 )         {             GoodPositions[SetBitCount] = i;             SetBitCount++;         }      Random r = new Random(); // Should use one global instance      return GoodPositions[r.Next(SetBitCount)]; } 

    I’m afraid it won’t get any better than that. Not unless you can somehow change the inputs/outputs or requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Implement onPause() in your activity and call finish() on your… May 11, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer No; But I don't think it is restrictive. Maybe you… May 11, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer You could try something like this in the ServerValidate event… May 11, 2026 at 9:43 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.