Click here to Skip to main content
15,899,754 members
Home / Discussions / C#
   

C#

 
GeneralRe: random selection from a list Pin
james bradbery5-Mar-12 16:30
james bradbery5-Mar-12 16:30 
GeneralRe: random selection from a list Pin
RobCroll5-Mar-12 17:08
RobCroll5-Mar-12 17:08 
GeneralRe: random selection from a list Pin
james bradbery5-Mar-12 17:47
james bradbery5-Mar-12 17:47 
GeneralRe: random selection from a list Pin
james bradbery5-Mar-12 17:56
james bradbery5-Mar-12 17:56 
GeneralRe: random selection from a list Pin
RobCroll6-Mar-12 20:07
RobCroll6-Mar-12 20:07 
AnswerRe: random selection from a list Pin
BobJanova5-Mar-12 22:17
BobJanova5-Mar-12 22:17 
GeneralRe: random selection from a list Pin
james bradbery10-Mar-12 18:22
james bradbery10-Mar-12 18:22 
QuestionSlope method optimisation Pin
A*****5-Mar-12 14:51
A*****5-Mar-12 14:51 
I'm trying to optimise the following Slope method so that it runs in as few clock cycles as possible.

Any Suggestions?

public static double?[] slopeImpl(double?[] vals, int offset)
{
    double y = 0;

    int iterableLength = vals.Length;

    int numValues = vals.Length;
    double invNumValues = 1.0 / (vals.Length - offset);
    double x = (double)(((numValues * (numValues - 1)) >> 1) - ((offset * (offset - 1)) >> 1));

    for (int i = offset; i < iterableLength; i++) {

        y += (vals[i].GetType() == typeof(System.DBNull)) ? 0.0 : (double)vals[i];
    }

    y = y * invNumValues;

    double v1 = 0.0;
    double v2 = 0.0;
    double v2HalfResult = 0.0;

    for (int i = offset; i < iterableLength; i++) {
        v2HalfResult = (i - x);
        v1 += (v2HalfResult) * ((double)vals[i] - y);
        v2 += (v2HalfResult) * (v2HalfResult);
    }

    double slope = v1 / v2;
    double intercept = y - slope * x;

    double?[] result = new double?[2];
    result[0] = slope;
    result[1] = intercept;
    return result;
}


What I have considered:
1. There are two loops which are iterating the same number of times; however I am not sure if it is possible to use one loop. The second loop is dependant on the value of y which is calculated in the first loop and the line after.

2. Use a table to calculate x which is of the form
n(n-1) /2. Possible, however the values array can contain more than 365 elements.

Thanks for any help.
My blog:[^]


modified 5-Mar-12 21:35pm.

AnswerRe: Slope method optimisation Pin
RobCroll5-Mar-12 18:24
RobCroll5-Mar-12 18:24 
AnswerRe: Slope method optimisation Pin
A*****5-Mar-12 23:12
A*****5-Mar-12 23:12 
AnswerRe: Slope method optimisation Pin
Luc Pattyn5-Mar-12 19:30
sitebuilderLuc Pattyn5-Mar-12 19:30 
GeneralRe: Slope method optimisation Pin
A*****5-Mar-12 23:17
A*****5-Mar-12 23:17 
AnswerRe: Slope method optimisation Pin
BobJanova5-Mar-12 22:04
BobJanova5-Mar-12 22:04 
QuestionSemantic music engine Pin
Abdul Salam Shaikh5-Mar-12 8:49
Abdul Salam Shaikh5-Mar-12 8:49 
AnswerRe: Semantic music engine Pin
PIEBALDconsult5-Mar-12 8:52
mvePIEBALDconsult5-Mar-12 8:52 
AnswerRe: Semantic music engine Pin
Pete O'Hanlon5-Mar-12 8:54
mvePete O'Hanlon5-Mar-12 8:54 
QuestionReturn Value From Async Call Pin
Kevin Marois5-Mar-12 6:15
professionalKevin Marois5-Mar-12 6:15 
AnswerRe: Return Value From Async Call Pin
Pete O'Hanlon5-Mar-12 6:45
mvePete O'Hanlon5-Mar-12 6:45 
AnswerRe: Return Value From Async Call Pin
Luc Pattyn5-Mar-12 8:12
sitebuilderLuc Pattyn5-Mar-12 8:12 
AnswerRe: Return Value From Async Call Pin
SledgeHammer015-Mar-12 10:08
SledgeHammer015-Mar-12 10:08 
GeneralRe: Return Value From Async Call Pin
Dave Kreskowiak5-Mar-12 11:34
mveDave Kreskowiak5-Mar-12 11:34 
AnswerRe: Return Value From Async Call Pin
Mycroft Holmes5-Mar-12 14:46
professionalMycroft Holmes5-Mar-12 14:46 
AnswerRe: Return Value From Async Call Pin
BobJanova5-Mar-12 22:01
BobJanova5-Mar-12 22:01 
QuestionWinform linked to Entity Data Model Pin
pmcm5-Mar-12 5:25
pmcm5-Mar-12 5:25 
QuestionVB input box in C# Pin
glennPattonWork35-Mar-12 5:23
professionalglennPattonWork35-Mar-12 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.