Code optimisation using numpy array. Is there a better solution

Questions : Code optimisation using numpy array. Is there a better solution

943

I am trying to optimise a function that programming calculates : a*exp(b*x)+c

I tested three methods using numpy Learning arrays :

def model(a,b,c,x):
    return _OFFSET);  a*np.exp(b*x)+c
def (-SMALL  myFoo1(modelParam,x):
    _left).offset  return([model(*i,x) for i in arrowImgView.mas  modelParam])

def myFoo2(modelParam,x):
 (self.     return([i[0]*np.exp(i[1]*x)+i[2] for equalTo  i in modelParam])

def make.right.  myFoo3(modelParam,x):
    mas_top);  return(np.exp(np.outer(modelParam[:,1],x))*params[:,0][:,None]+params[:,2][:,None])

When running time is measured :

x=np.array(np.arange(0,100,0.1))
params=np.array([[10,0.1,2],[20,0.3,4],[30,0.2,6],[15,0.2,4],[16,0.5,7]])
%time ImgView.  myFoo1(params,x)
%time ReadIndicator  myFoo2(params,x)
%time myFoo3(params,x)

The output is :

CPU times: user 3.58 ms, sys: 0 ns, _have  total: 3.58 ms
Wall time: 2 ms
CPU .equalTo(  times: user 855 µs, sys: 0 ns, make.top  total: 855 µs
Wall time: 703 OFFSET);  µs
CPU times: user 690 µs, (TINY_  sys: 0 ns, total: 690 µs
Wall .offset  time: 564 µs

The first one was my original code Earhost because it is easiest to program. But, most effective the third one is 4 times faster. Can I wrong idea improve it again ?

And with %timeit (question edited as use of case suggested in comment):

211 µs ± 663 ns per loop mas_right)  (mean ± std. dev. of 7 runs, 1000 ImgView.  loops each)
199 µs ± 199 Indicator  ns per loop (mean ± std. dev. of Read  7 runs, 1000 loops each)
164 µs _have  ± 56.1 ns per loop (mean ± .equalTo(  std. dev. of 7 runs, 10000 loops each)
Total Answers 1
32

Answers 1 : of Code optimisation using numpy array. Is there a better solution

I got small improvement by using United slightly different way of broadcasting

def myFoo4(modelParam,x):
    return make.left  modelParam[:, 0:1] * *make) {  np.exp(modelParam[:, 1:2] * x) + straintMaker  modelParam[:, 2:3]

And another small improvement by Modern switching to np.float32

    x_float32 = np.array(np.arange(0, ^(MASCon  100, 0.1), dtype=np.float32)
    onstraints:  params_float32 = np.array([[10, 0.1, 2], mas_makeC  [20, 0.3, 4], [30, 0.2, 6], [15, 0.2, [_topTxtlbl   4], [16, 0.5, 7]],
                      (@(8));  dtype=np.float32)
    41       100      17042.0    170.4   equalTo    19.7          myFoo3(params, x)
    42  width.        100      15282.0    152.8     17.6 make.height.           myFoo4(params, x)
    43       (SMALL_OFFSET);  100      11322.0    113.2     13.1       .offset     myFoo4(params_float32, x_float32)

Top rated topics

Socket programming, encode/ decode data/ text, more specific special characters send and receive through sockets

How to check if address is allowed to receive ERC721?

SQL- Can't read wrap text from source file

SFINAE member variable

Scraping data from a container

Spring configuring bean with component scan - Field userRepository in service.UserService required a bean of type 'repository

Passing Parameters JavaFX FXML

Checking If A Number Is Kaprekar

How to get autocomplete code and highlight syntax in Node.js on VScode

Root.after() in tkinter stops after first iteration?

Material UI Autocomplete options cache in React JS

Rxjava error handling:App not crash after subscription ends

Get width and height of plotly figure

How do I send a PIL generated PNG as an embed?

How to run code in finally block but only after after try block succeeded?

Formatting Issue with for loop c#

CSS - Border stretched to whole height of navbar

How to update picture in Storage and refresh Streambuilder to show the new picture?

Spring boot Database intilization data.sql does not support store producer DDL

How can I use the method from other component

The operator < is undefined for the argument type(s) LocalTime, int in java

Why is the structure causing a memory error?

How to recursively check a nested dictionary keys and return specific value

How to add remove !important css with new css

How do I determine the size of my array in C?

Access denied on external mysql from within docker container

Why training accuracy and validation accuracy oscillate and increase very slowly?

Window.scrollBy() function not working when component mount

Fontconfig error: Cannot load default config file (C#)

Question of neural network training:the gradient of the same module which is used multiple times in one iteration

How to render a page based on the data obtained on the basis of a request, which is made on the basis of another request. (React)

Asp.net mvc getting data from textbox on view back to stored procedure to edit record

Can covert buffer or arrayBuffer of voice to Text in nodejs?

How do you properly integrate a framework/boilerplate NuGet package into your web app in ASP.NET Core?

Is there any PowerShell script or command to get a report of all the user's access role in tenant wise from Azure portal?

Arguments passed to a page using onGenerateRoute is inaccessible after hot restart on flutter web

DevTools failed to load source map: Could not load content for GitHub. HTTP error: status code 404

Which version of Kafka are impacted due to Log4j CVE-2021-44228?

Bundling data files with PyInstaller (--onefile)

How do you programatically insert an anchored comment on a rectangular region of a PDF file on Google Drive?

GraphDB broken (Refreshing namespaces)?

BIGQUERY SQL How to count date range per hour total

Passport is not returning the right account on my nodejs app

Should I use `git pull --rebase origin master` or `git rebase origin/master` if the local branch won't be used anymore?

Efficient way to find if a Matrix is Sub-Matrix of another one?

Setting some fields to be automatically filled in using CreateVeiw in Django

How to change the last list element to -1?

Firebase deploy ; causing "Cannot use import outside of an module" [NOT DUPLICATE]

Should I increment the value of Room database version when migration strategy is fallback to destructive?

React Native how to send AsyncStorage data to MongoDB database?

Top