Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107539
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Derek Klinge <schilke.60@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Optimizing Memory Allocation in a Simple, but Long Function |
| Date | Sun, 24 Apr 2016 03:05:45 +0000 |
| Lines | 50 |
| Message-ID | <mailman.29.1461478406.32212.python-list@python.org> (permalink) |
| References | <CAGuRns8KhOFL-a1_a=0hMP=NbktoJw78ih3j4uUSbvkF8bf3pw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de yGyeVMSgGNOY3Ocl4QfYagmWlaSbNlhL2E1TWKrJtzHw== |
| Return-Path | <schilke.60@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'algorithms,': 0.07; 'linear': 0.07; 'subject:Function': 0.07; 'skip:\\ 40': 0.09; 'subject:Long': 0.09; 'python': 0.10; 'def': 0.13; 'importing': 0.15; 'received:io': 0.16; 'received:psf.io': 0.16; 'unneeded': 0.16; 'memory': 0.17; 'module,': 0.18; 'skip:l 30': 0.18; '>': 0.18; 'math': 0.20; 'to:name:python-list@python.org': 0.20; 'bit': 0.23; 'seems': 0.23; 'thanks,': 0.24; 'import': 0.24; 'script': 0.25; 'message-id:@mail.gmail.com': 0.27; 'skip:e 30': 0.27; 'faster,': 0.29; "i'm": 0.30; 'becomes': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'optimize': 0.33; 'subject:Simple': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'quite': 0.35; 'received:74.125.82': 0.35; 'sometimes': 0.35; 'step': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'method': 0.37; 'skip:s 40': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'skip:n 10': 0.62; 'repeat': 0.67; 'hoping': 0.77; 'insight': 0.79 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=vwTNXnpmJvLnkvnCl+73jvx47RLJsESuoy7JtvOxH4Y=; b=qvIgqabhqLP/ZO+YTY2Qrd/8YaiU+RfjQi68a9N7r0YX02F/n5lxSJpMtt6akN9Di6 mAqpAfZSZAj11EGncUGNtW5YKBwDCAgg0qyxeafmKu632a8HCUsILspVajWlwoPQkCTo A/MKz8FoGujQWDzUxImS2ZTuXJa+V2E2CbOiLtmHIB+4cJvCYTQlcCXtrKy3twfTsH4M dMIrhCX8yYSxQxlhciJYHD2aA/6uVaJTtNEvOALZE1hQwC89iuLn9fevJwr3qq3OVlHQ swpfvjGhc6ea4IAuCPrBsE4hAdZjGAv3OJO/PR2X1PEipeTCG4M97wWg1amQKKFBDgHv mlFw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=vwTNXnpmJvLnkvnCl+73jvx47RLJsESuoy7JtvOxH4Y=; b=NOIzQLhlcK291m0LY4S0LMDVzGVgrSRD8ZAYzWCefVhtRbE37LUL2l5bcUr8aTdcF6 lDtbWQv74fSy/ppIgM6Xs7EfVPBQnR/XbqP8F27f/z2uNo0D9tTvmxpDM+m/Ix5Jbp3M wO4QXWstwPm7ioLGUrpU96g6lYscSAPV0Xy/fkp+CVyAf1NQBxt7EcjffAr8IuTvK8xq MPnzGVje2ueaO3SzJhylE4SrEQGzIMnxBB43inRi1Ljgpwo0wUPzd55wk8l0pWOeiMcj DjtIYQz0tRKOtQdHaudvIHO/R0cLI/bRQ9uVRfsfPT2GDtUzmjQ4R4kJfefTQrePZu9C ug+Q== |
| X-Gm-Message-State | AOPr4FW25Uq+O6IRK2yMra1HBj7htflxq2SygFzN62D27y5IrLsmNnNAgORPOq99TKdTh83/laZ2akzX85Oilg== |
| X-Received | by 10.194.16.8 with SMTP id b8mr27148551wjd.51.1461467155136; Sat, 23 Apr 2016 20:05:55 -0700 (PDT) |
| X-Mailman-Approved-At | Sun, 24 Apr 2016 02:13:24 -0400 |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.22 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAGuRns8KhOFL-a1_a=0hMP=NbktoJw78ih3j4uUSbvkF8bf3pw@mail.gmail.com> |
| Xref | csiph.com comp.lang.python:107539 |
Show key headers only | View raw
I have been writing a python script to explore Euler's Method of
approximating Euler's Number. I was hoping there might be a way to make
this process work faster, as for sufficiently large eulerSteps, the process
below becomes quite slow and sometimes memory intensive. I'm hoping someone
can give me some insight as to how to optimize these algorithms, or ways I
might decrease memory usage. I have been thinking about finding a way
around importing the math module, as it seems a bit unneeded except as an
easy reference.
## Write a method to approximate Euler's Number using Euler's Method
import math
class EulersNumber():
def __init__(self,n):
self.eulerSteps = n
self.e = self.EulersMethod(self.eulerSteps)
def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d
def EulersMethod(self, numberOfSteps): # Repeat linear approximation over
an even range
e = 1 # e**0 = 1
for step in range(numberOfSteps):
e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
f'(x)=f(x)
return e
def EulerStepWithGuess(accuracy,guessForN):
n = guessForN
e = EulersNumber(n)
while abs(e.e -math.e) > abs(accuracy):
n +=1
e = EulersNumber(n)
print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e-math.e)))
return e
def EulersNumberToAccuracy(PowerOfTen):
x = 1
theGuess = 1
thisE = EulersNumber(1)
while x <= abs(PowerOfTen):
thisE = EulerStepWithGuess(10**(-1*x),theGuess)
theGuess = thisE.eulerSteps * 10
x += 1
return thisE
Thanks,
Derek
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Optimizing Memory Allocation in a Simple, but Long Function Derek Klinge <schilke.60@gmail.com> - 2016-04-24 03:05 +0000
csiph-web