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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:36:23+00:00 2026-06-02T16:36:23+00:00

Is there anyway to make it work? func=i_want_it_to_cache_everything(lambda a,b:a+b) And it has to be

  • 0

Is there anyway to make it work?

func=i_want_it_to_cache_everything(lambda a,b:a+b)

And it has to be done in one line…

Update2:

I figured out the solution (thanks to everyone who replied!).
But… There is an interesting phenomenon: caching slows down program?

import functools,datetime,timeit
@functools.lru_cache(maxsize=50000)
def euclidean_distance3(p1,p2):
    return (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2
euclidean_distance=(functools.lru_cache(maxsize=50000)(lambda p1,p2: (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2))
euclidean_distance2=lambda p1,p2: (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2+(p1[3]-p2[3])**2
print(datetime.datetime.now())
def test1():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance((i,i,i,i),(j,j,j,j));
def test2():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance2((i,i,i,i),(j,j,j,j));
def test3():
    for z in range(50):
        for i in range(200):
            for j in range(200):
                euclidean_distance3((i,i,i,i),(j,j,j,j));
t1=timeit.Timer(test1)
print(t1.timeit(1))
t2=timeit.Timer(test2)
print(t2.timeit(1))
t3=timeit.Timer(test3)
print(t3.timeit(1))

print(euclidean_distance.cache_info())
print(euclidean_distance3.cache_info())

output:

9.989034592910151
4.936129879313011
10.528836308312947
CacheInfo(hits=1960000, misses=40000, maxsize=50000, currsize=40000)
CacheInfo(hits=1960000, misses=40000, maxsize=50000, currsize=40000)
  • 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-06-02T16:36:24+00:00Added an answer on June 2, 2026 at 4:36 pm
    >>> from functools import lru_cache
    >>> x = lru_cache()(lambda a,b:a+b)
    >>> x(2,3)
    5
    >>> x(4,2)
    6
    >>> x(2,3)
    5
    >>> x.cache_info()
    CacheInfo(hits=1, misses=2, maxsize=100, currsize=2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway for me to make it work so anyone who presses F5
Is there any way to make this work in Java? public static void change(List<?
MyClass c = 10; Is there any way to make this code work? I
Is there anyway to make a secondary key in SQL? Lets say I have
echo ddayaynightday | sed 's/day//g' It ends up daynight Is there anyway to make
In VB.net is there anyway to make a certain part of the string to
Is there anyway to make a div box shake on page load? Like maybe
Is there any way to make a code like this work: public class Func2<A,
Is there anyway to make an image map W3C XHTML 1.0 Strict compliant? I'm
Is there anyway to specify more than one MergeSection value for the MSBuild LINK

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.