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

  • Home
  • SEARCH
  • 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 6689681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:52+00:00 2026-05-26T05:32:52+00:00

Possible Duplicate: C#. Need to optimise counting positive and negative values I need to

  • 0

Possible Duplicate:
C#. Need to optimise counting positive and negative values

I need to maximize speed of the following functionality:

  • a. a value comes in. value has 2 properties – int value and long timestamp in ticks.
  • b. need to count previously stored values which are younger than 1ms (from the current).
  • c. need to count negative and positive separately.
  • d. i only need to know if there are either 10 neg or pos values. i dont need to keep any other knowledge of the values.

me thinks – to implement 2 ring arrays for pos and neg separately, replacing expired with 0 keeping track of pos.neg counts as they come.

any thoughts?

  • 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. Editorial Team
    Editorial Team
    2026-05-26T05:32:53+00:00Added an answer on May 26, 2026 at 5:32 am

    Maintaining 2 buffers to keep the positives separated from the negatives sounds like a pain and inefficient.

    You could instead have a single buffer with all the values, and use std::accumulate to count up the positives and negatives. If you start with a collection of all the tuples (each of which has an age and a value), you could begin by sorting the collection according to age, finding the last element that is <= 1 ms old, and then using accumulate from begin() to that point. Here’s some code that demonstrates that last bit:

    #include <algorithm>
    #include <numeric>
    #include <iterator>
    #include <vector>
    #include <string>
    #include <ctime>
    using namespace std;
    
    struct Counter 
    {
        Counter(unsigned pos=0, unsigned neg=0) : pos_(pos), neg_(neg) {};
        unsigned pos_, neg_;
        Counter& operator+(int n)
        {
            if( n < 0 )
                ++neg_;
            else if( n > 0 )
                ++pos_;
            return * this;
        }
    };
    
    int main()
    {
        srand((unsigned)time(0));
    
        vector<int> vals;
        generate_n(back_inserter(vals), 1000, []() 
        {
            return (rand() / (RAND_MAX/40)) - 20;
        });
    
        Counter cnt = accumulate(vals.begin(), vals.end(), Counter());
    }
    

    If sorting the collection by age and then searching the sorted results for the last eligible entry sounds too ineficient, you could use for_each_if instead of accumulate and simply iterate over the whole collection once. for_each_if isn’t part of the Standard Library, but it’s easy enough to write. If you don’t want to muck about with writing your own for_each_if that’s fine, too. You could simply tweak the accumulator a bit so that it doesn’t accumulate elements which are too old:

    #include <algorithm>
    
    #include <numeric>
    #include <iterator>
    #include <vector>
    #include <string>
    #include <ctime>
    using namespace std;
    
    struct Tuple
    {
        int val_;
        unsigned age_;
    };
    
    struct Counter 
    {
        Counter(unsigned pos=0, unsigned neg=0) : pos_(pos), neg_(neg) {};
        unsigned pos_, neg_;
        Counter& operator+(const Tuple& tuple)
        {
            if( tuple.age_ > 1 )
                return * this; 
    
            if( tuple.val_ < 0 )
                ++neg_;
            else if( tuple.val_ > 0 )
                ++pos_;
    
            return * this;
        }
    };
    
    int main()
    {
        srand((unsigned)time(0));
    
        vector<Tuple> tuples;
        generate_n(back_inserter(tuples), 1000, []() -> Tuple
        {
            Tuple retval;
            retval.val_ = (rand() / (RAND_MAX/40)) - 20;
            retval.age_ = (rand() / (RAND_MAX/5));
            return retval;
        });
    
        Counter cnt = accumulate(tuples.begin(), tuples.end(), Counter());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: Need help with getline() In the following code, my getline is skipped
Possible Duplicate: Property Name and need its value Hi, This one should be easy
Possible Duplicate: Multi value Dictionary I just need to store 2 value. How can
Possible Duplicate: Do I need to disable NSLog before release Application? I wonder if
Possible Duplicate: Change CSS Dynamically I need to change the height of a div
Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: When do I need to specify the JavaScript protocol? for example <body
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I need
Possible Duplicate: how to hide a div after some time period? i need to

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.