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 7629027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:43:51+00:00 2026-05-31T05:43:51+00:00

I was wondering if someone could help me with the performance of this code

  • 0

I was wondering if someone could help me with the performance of this code snippet in Clojure 1.3. I am trying to implement a simple function that takes two vectors and does a sum of products.

So let’s say the vectors are X (size 10,000 elements) and B (size 3 elements), and the sum of products are stored in a vector Y, mathematically it looks like this:

Y0 = B0*X2 + B1*X1 + B2*X0

Y1 = B0*X3 + B1*X2 + B2*X1

Y2 = B0*X4 + B1*X3 + B2*X2

and so on …

For this example, the size of Y will end up being 9997, which corresponds to (10,000 – 3). I’ve set up the function to accept any size of X and B.

Here’s the code: It basically takes (count b) elements at a time from X, reverses it, maps * onto B and sums the contents of the resulting sequence to produce an element of Y.

(defn filt [b-vec x-vec]
  (loop [n 0 sig x-vec result []]
    (if (= n (- (count x-vec) (count b-vec)))
      result
      (recur (inc n) (rest sig) (conj result (->> sig
                                                  (take (count b-vec))
                                                  (reverse)
                                                  (map * b-vec)
                                                  (apply +)))))))

Upon letting X be (vec (range 1 10001)) and B being [1 2 3], this function takes approximately 6 seconds to run. I was hoping someone could suggest improvements to the run time, whether it be algorithmic, or perhaps a language detail I might be abusing.

Thanks!

P.S. I have done (set! *warn-on-reflection* true) but don’t get any reflection warning messages.

  • 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-31T05:43:53+00:00Added an answer on May 31, 2026 at 5:43 am

    You are using count many times unnecessary. Below code calculate count one time only

    (defn filt [b-vec x-vec]
      (let [bc (count b-vec) xc (count x-vec)]
        (loop [n 0 sig x-vec result []]
            (if (= n (- xc bc))
              result
              (recur (inc n) (rest sig) (conj result (->> sig
                                                      (take bc)
                                                      (reverse)
                                                      (map * b-vec)
                                                      (apply +)))))))) 
    
    
    (time (def b (filt [1 2 3] (range 10000))))
    => "Elapsed time: 50.892536 msecs"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could help me. Im trying to integrate some code
I was wondering if someone could help me debug the following OCaml code that
I am wondering if someone could help me with some probably rather simple code,
Just wondering if someone could help me with a very simple SQL query. I
I was wondering if someone could help me with this. I have defined my
I'm wondering if someone could look over my code. I'm trying to pass a
was wondering if someone could help with this one. This is the first time
I was wondering if someone could help with this: I have a series of
I was wondering if someone could help with with some jquery code for doing
I was wondering if someone could help me understand this problem. I prepared a

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.