473,756 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allocating an array.array of a specific length in pyrex

Hi,

I have to do some data manipulation that needs to be fast. I had a
generator approach (that was faster than a list approch) which was
taking approximately 5 seconds to run both encode and decode.

I have done a conversion to pyrex and have gotten it down to about 2
seconds to run both encode and decode. An an excerpt from the pyrex
code is included below. My question is, is there a better way to
allocate the memory for the array.array object than:
a = array.array('B' , [0] * (pixels * 2))

I already know what the lenght is going to be do I have to give it a
sequence(in this case a list of zeros) or is there a C interface that
I can use to tell array to allocate memory for pixels*2 unsigned
chars.

I would prefer to not get into using Numeric or Numarray(I know I can
do it with them).

Yes the algorithm is functionally correct. I know I am throwing away
data. Rest assured that the entropy has been changed such that the
data thrown away does not matter (too much).

Thanks
-Chris
import array
cdef extern from "Python.h":
int PyObject_AsWrit eBuffer(object obj, void **buffer, int *buffer_len)
int PyObject_AsRead Buffer(object obj, void **buffer, int *buffer_len)

cdef int get_w_buffer(ob j, unsigned char **data, int *length) except -1:
cdef int result
cdef void *vd
# use a void *
result = PyObject_AsWrit eBuffer(obj, &vd, length)
data[0] = <unsigned char *>vd
return result

cdef int get_r_buffer(ob j, unsigned char **data, int *length) except -1:
cdef int result
cdef void *vd
# use a void *
result = PyObject_AsRead Buffer(obj, &vd, length)
data[0] = <unsigned char *>vd
return result

def encode(int w, int h, in_a):
cdef unsigned int pixels, a_off, str_off
cdef int res, inlen, buffer_len
cdef unsigned char *in_str, *out_str

res = get_r_buffer(in _a, &in_str, &inlen)
if res:
raise Exception, "Could not get a readable buffer from the input"
pixels = w * h
a = array.array('B' , [0] * (pixels * 2))
res = get_w_buffer(a, &out_str, &buffer_len)
if res:
raise Exception, "Could not get a writeable buffer to write to"

str_off = 0
a_off = 0
while a_off < buffer_len and str_off < inlen:
out_str[a_off] = in_str[str_off]
out_str[a_off+1] = (in_str[str_off+1] + in_str[str_off+4])/2
out_str[a_off+2] = in_str[str_off+3]
out_str[a_off+3] = (in_str[str_off+2] + in_str[str_off+5])/2

str_off = str_off + 6
a_off = a_off + 4


return a

--
Christopher Lambacher
la******@comput er.org
Jul 19 '05 #1
0 2837

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2352
by: umdehoon | last post by:
Hello everybody, Say I have an array (Python array, not Numpy) that contains characters representing a DNA sequence. Something like >>> from array import array >>> s = array('c', "ATAGCTGCT") Now I want to calculate the reverse complement of this sequence. I could make a simple loop:
8
3866
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
7
3266
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
35
6664
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
2
6860
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
7
2049
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not clear how to do this.
2
2515
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
10
1767
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
13
1996
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to have unfreeable (but still technically reachable) memory hanging around but it is, I think still a useful thing to do: void fn(int *stuff, int nstuffs) { static int *temp_stuff = NULL; static int ntemp_stuffs = 0;
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
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,...
0
9722
tracyyun
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...
0
8723
agi2029
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...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
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();...
0
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2677
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.