def f(x,y):
return math.sin(x*y) + 8 * x
I have code like this:
def main():
n = 2000
a = zeros((n,n), Float)
xcoor = arange(0,1,1/float(n))
ycoor = arange(0,1,1/float(n))
for i in range(n):
for j in range(n):
a[i,j] = f(xcoor[i], ycoor[j]) # f(x,y) = sin(x*y) + 8*x
print a[1000,1000]
pass
if __name__ == '__main__':
main()
I try to make this run faster even using psyco, but I found this still
slow, I tried using java and found it around 8x faster...
public class s1 {
/**
* @param args
*/
public static int n = 2000;
public static double[][] a = new double[n][n];
public static double [] xcoor = new double[n];
public static double [] ycoor = new double[n];
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i=0; i<n; i++){
xcoor[i] = i/(float)(n);
ycoor[i] = i/(float)n;
}
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
a[i][j] = f(xcoor[i], ycoor[j]);
}
}
System.out.prin tln(a[1000][1000]);
}
public static double f(double x, double y){
return Math.sin(x*y) + 8*x;
}
}
Can anybody help?
pujo 6 1827 aj****@gmail.co m wrote: def f(x,y): return math.sin(x*y) + 8 * x I have code like this:
def main(): n = 2000 a = zeros((n,n), Float) xcoor = arange(0,1,1/float(n)) ycoor = arange(0,1,1/float(n))
for i in range(n): for j in range(n): a[i,j] = f(xcoor[i], ycoor[j]) # f(x,y) = sin(x*y) + 8*x
print a[1000,1000] pass
if __name__ == '__main__': main()
Ufuncs are your friend:
from scipy import *
def f(x, y):
return sin(x*y) + 8*x
def main():
n = 2000
ycoor = linspace(0.0, 1.0, n)
xcoor = transpose(atlea st_2d(ycoor))
a = f(xcoor, ycoor)
print a[1000, 1000]
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
It looks like you're using Numeric for your arrays, but you are then
pulling sin from the math module and calculating one point at a time.
Instead try using sin(whole array) where sin is a ufunc from the
Numeric module. Also, it's usually not good practice to "import
Numeric as *". Instead try import Numeric as N so it's clear which
functions you are using.
-- David aj****@gmail.co m wrote: def f(x,y): return math.sin(x*y) + 8 * x I have code like this:
def main(): n = 2000 a = zeros((n,n), Float) xcoor = arange(0,1,1/float(n)) ycoor = arange(0,1,1/float(n))
for i in range(n): for j in range(n): a[i,j] = f(xcoor[i], ycoor[j]) # f(x,y) = sin(x*y) + 8*x
print a[1000,1000] pass
if __name__ == '__main__': main()
I try to make this run faster even using psyco, but I found this still slow, I tried using java and found it around 8x faster...
I guess the double loop (4E6 rounds) makes your program so slow.
I assume you are using numarray or numeric for this.
The built-in array operations are a lot faster.
Try using them instead. And function calls are not free either.
Xcoor and ycoor are equal, so there is no need to generate them both.
I guess the following would be a lot faster:
def func():
n = 2000
a = numarray.zeros( (n,n), "Float")
coor = numarray.arange (0,1,1/float(n))
for i in range(n):
a[:,i] = numarray.sin(co or*coor[i]) + 8*coor
print a[1000,1000]
pass
hello,
I found that scipy only works with python 2.3 or?
I don't know if the logic is correct:
1. loop inside loop uses a lot of resources
2. Numeric or Numpy can make program faster
3. It use kind of Array/Matrix analysis style
4. We have to change our algorithms so that Numeric or Numpy can help
us, Matrix style
Best Regards,
pujo
<aj****@gmail.c om> wrote: hello,
I found that scipy only works with python 2.3 or?
You can use Numeric instead of scipy if you need/want to:
from Numeric import arange,reshape, sin
def computeMatrix(n ):
xcoor = arange(0,1,1/float(n))
ycoor = reshape(xcoor, (n,1))
return sin(xcoor*ycoor ) + 8*xcoor
Note that arange() does not include the endpoint, i.e. arange(0,1,0.25 ).tolist() ==[0.0, 0.25, 0.5,
0.75].
I don't know if the logic is correct: 1. loop inside loop uses a lot of resources 2. Numeric or Numpy can make program faster 3. It use kind of Array/Matrix analysis style 4. We have to change our algorithms so that Numeric or Numpy can help us, Matrix style
That's correct more or less.
George
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"aj****@gmail.c om" <aj****@gmail.c om> wrote: def f(x,y): return math.sin(x*y) + 8 * x I have code like this:
def main(): n = 2000 a = zeros((n,n), Float) xcoor = arange(0,1,1/float(n)) ycoor = arange(0,1,1/float(n))
for i in range(n): for j in range(n): a[i,j] = f(xcoor[i], ycoor[j]) # f(x,y) = sin(x*y) + 8*x
print a[1000,1000] pass
if __name__ == '__main__': main()
I would guess that you are spending most of your time calculating
sin(x*y). To find out, just replace f(x,y) with 1, which will produce
wrong results really fast, and see what that does to your execution time.
_______________ _______________ _______________ _______________ ____________
TonyN.:' *firstname*nlsn ews@georgea*las tname*.com
' <http://www.georgeanels on.com/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kamilche |
last post by:
I have a routine that I really need, but it slows down processing
significantly. Can you spot any ineffeciencies in the code?
This code makes a critical function of mine run about 7x slower than
using a prebuilt format string. For maximum flexibility, it would be
best to calculate the format string using this method, so I'd dearly
love to keep it.
def fmtstring(args):
delim = '\0'
|
by: Brent Patroch |
last post by:
Hello,
Novice here, I am doing bulk emails using CDO, connection to a smtp
server at another location. I am trying to streamline my script, or
through it out and start over to make it faster.
I know that I am doing something wrong and that messages should be
sending much faster. Any help appreciated:
Set objConfig = Server.CreateObject("CDO.Configuration")
|
by: Scott Emick |
last post by:
I am using the following to compute distances between two lat/long
coordinates for a store locator - (VB .NET 2003)
it seems to take a long time to iterate through like 100-150 locations -
about 10-15 seconds...I want to make the code faster. I changed it to be
multi-threaded, and it doesn't really make it any faster. The bottleneck
seems to be with the math computations.
Any ideas like changing my data types or other ideas etc would...
|
by: Niyazi |
last post by:
Hi
I have a report that I have to run it monthly in my machine.
My code in VB.NET and I access AS400 to get data, anaysie it and send into
pre formated Excel sheet. The data consist of 9000 rows.
I use data table and with for loop I send the data row by row in
pre-formated Excel sheet.
My machine is:
|
by: Extremest |
last post by:
I know there are ways to make this a lot faster. Any
newsreader does this in seconds. I don't know how they do
it and I am very new to c#. If anyone knows a faster way
please let me know. All I am doing is quering the db for
all the headers for a certain group and then going through
them to find all the parts of each post. I only want ones
that are complete. Meaning all segments for that one file
posted are there.
using System;
| |
by: vunet.us |
last post by:
Is there a suggestion I can make this code run faster:
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
....................
if(document.getElementById("n")){ doN(); }
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:
|
by: Simply_Red |
last post by:
Hi,
is there a way to make this function faster???
struct Points {
double X;
double Y;
};
|
by: istillshine |
last post by:
When I used gprof to see which function consumed most running time, I
identified the following one. sz was less than 5000 on average, but
foo had been called about 1,000,000 times. I have tried using
"register sum = 0.0" and saw some improvement. My question is how to
improve foo further to make it faster.
double foo(double *a, double *b, int sz)
{
double sum = 0.0;
|
by: Steve Bergman |
last post by:
I'm involved in a discussion thread in which it has been stated that:
"""
Anything written in a language that is 20x slower (Perl, Python,
PHP) than C/C++ should be instantly rejected by users on those grounds
alone.
"""
I've challenged someone to beat the snippet of code below in C, C++,
or assembler, for reading in one million pairs of random floats and
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |