Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ctypes import *
- import mmap
- import struct
- import timeit
- class SgnHeader(Structure):
- _pack_ = 1
- _fields_ = [ ("ID", c_char * 64),
- ("headLen", c_ulong),
- ("RemarkLen", c_ulong),
- ("usPackingType", c_ushort),
- ("Squared", c_ushort),
- ("SampleSize", c_ushort),
- ("FreqDiskr", c_double),
- ("dBand", c_double),
- ("dCF", c_double),
- ("tSamplesOrigin", c_ulonglong) ]
- class Ipp16sc(Structure):
- _pack_ = 1
- _fields_ = [ ("re", c_short), ("im", c_short) ]
- headerLen = sizeof( SgnHeader )
- sampleLen = sizeof( Ipp16sc )
- def doit():
- global headerLen
- global sampleLen
- f = open( "d:/InComing/Analiz/file/0_tone.sgn", "r+b" )
- m = mmap.mmap( f.fileno(), 0 )
- header = SgnHeader.from_buffer_copy( m[:headerLen] )
- samplesCount = ( len( m ) - headerLen ) / sampleLen
- s = Ipp16sc * samplesCount
- samples = s.from_buffer_copy( m[headerLen:] )
- for i in samples:
- i.re * i.re + i.im * i.im
- m.close()
- f.close()
- def doit2():
- global headerLen
- global sampleLen
- f = open( "d:/InComing/Analiz/file/0_tone.sgn", "r+b" )
- f.seek(0, 2)
- fileSize = f.tell()
- f.seek(0)
- #print fileSize
- headerBytes = f.read( headerLen )
- header = SgnHeader.from_buffer_copy( headerBytes )
- structFmt = "=2h"
- s = struct.Struct( structFmt )
- samples = []
- samplesCount = ( fileSize - headerLen ) / sampleLen
- for i in range(samplesCount):
- sampleBytes = f.read( sampleLen )
- samples.append( s.unpack( sampleBytes ) )
- for i in samples:
- i[0] * i[0] + i[1] * i[1]
- f.close()
- from timeit import Timer
- t = Timer( "doit()", "from __main__ import doit" )
- print t.timeit( number = 10 )
- t = Timer( "doit2()", "from __main__ import doit2" )
- print t.timeit( number = 10 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement