I have an array of integers and a number n.  I want to sum the elements of the array and return the index where the sum passes n, as efficiently as possible.For example, assume:theString = 6-7-7-10-7-6theNumber = 33expected result: 5This does the trick but not very efficiently: (and error prone; will check for errors when I've found the best solution)Public Function getIndexFromSum(theString As String, theNumber As Integer) As Integer    Dim theSides As Variant    Dim sum As Integer    Dim index As Integer    theSides = Split(theString, -)    While sum <= theNumber        sum = sum + theSides(index)        index = index + 1    Wend    getIndexFromSum = indexEnd FunctionI haven't been able to describe this to google succinctly enough to get any meaningful results, and searching for any combination of the keywords involved produces links to tons of unrelated stuff.  I'm sure this problem will have been encountered before (and likely has a smart solution out there somewhere), but I haven't been able to find any sites that can help.Any help/advice would be very much appreciated!EDIT - more info1) theString will always be stored in the format x-x-x.2) theNumber will always be smaller than or equal to the sum of the numbers in theString.3) The length of theString will be short - usually between 2 and 10 numbers, each less than 100.4) The function will be called with small inputs, but many times.  Low overhead is top priority.
Return index of array where sum first passes n
array;vba
One inefficient line of code is this one:Split(theString, -)It's slow because:It splits the entire string, even though you're only interested in the first part of the string.It allocates a new array and creates many new string objects, instead of parsing the existing string in-place.Therefore a faster algorithm, if the string is longer than you need, might be:   while not found       look for the next hyphen       get the substring up to the next hyphen       convert the substring to an integer       add the number       exit the loop if the sum is large enoughHowever, if you need to parse the whole (or most of) the string, the implementation of Split might be faster than your implementation of the above algorithm.Another possible inefficiency is the conversion from string to number. My guess is that VB.NET implicitly uses CInt which is a complicated function. Another method like Convert.ToInt32 or Int32.Parse might be faster, but even that might be slow because, according to MSDN,... value is interpreted by using the formatting conventions of the current thread culture.Other people get faster results by writing their own converters.In summary (further to Comintern's suggestion) I suspect that something like the following would be faster (forgive me if I code this in C# instead of VB) (untested code ahead):int number = 0;int total = 0;int count = 0;foreach (char c in theString) // For Each c As Char in theString{    if (c == '-')    {        total = total + number;        if (total > theNumber)            return count;        // else prepare for the next number        count = count + 1;        number = 0;    }    else    {        // continue to parse this number        // assume that c is an ASCII digit        int digit = (c - '0'); // But is converting char to int slow or difficult?        number = (10 * number) + digit;    }}// didn't find the number; return count anyway?return count;