Reducing Computation Time

Hey I have somewhat tricky task of reducing computation time for one line of code in my professor's project. Is there any way to reduce the following code so that it computes at a faster rate (maybe 0.01s), right now the time to beat is 0.08s. The code is as follows:
(32 bit IEEE demultiplexed)
x = hex2float( dec2hex( fread(fid, 1, 'ubit32', 0, 'ieee-be') ))
Basicslly looking for quicker way to convert the binary file to float. If you're not sure, I'm open to specifics suggestions. Also, would it be better if I wrote a code like this in C first and then let matlab read from there? Thank you.

 채택된 답변

Jan
Jan 2012년 4월 26일

1 개 추천

d = fread(fid, 1, 'uint32', 'ieee-be');
x = hex2float(sprintf('%x', d));
I do not have hex2float. Could you provide some example data? I assume typecast(uint32(d), 'single') will help also, if the format of fread is set accordingly. But without hex2float I cannot test this.
Anyhow, 0.08 sec sounds extremely slow. I assume this is cause by the rest of the code.

댓글 수: 4

Walter Roberson
Walter Roberson 2012년 4월 26일
hex2float() is in the FEX.
Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 26일
Sorry, I was only given that piece of code to work with, cannot supply/comment on rest of code. All I know is what is listed, need to read in a binary file and produce
Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 26일
*produce float numbers eventually
Jan
Jan 2012년 4월 30일
Thanks, Walter. I cannot find it in the FEX: http://www.mathworks.com/matlabcentral/fileexchange/?sort=date_desc_updated&term=hex2float

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 4월 26일

1 개 추천

Is there a reason you are not just using
x = fread(fid, 1, '*float32', 0, 'ieee-be')
Also if you are doing a lot of calculations, it is almost always faster to use fread() to read a number of items at once. (If you stick with going through hex, then dec2hex() can be used on arrays.)

댓글 수: 6

Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 26일
Yes, the file Im reading is binary with multiple formats. Does that change anything or is this still applicable?
Walter Roberson
Walter Roberson 2012년 4월 27일
Multiple binary formats should not affect my suggestion for reading a single 32 bit float.
Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 27일
So just to confirm, this code reads in binary and converts to float and conserves the same answer if I had convert to hex first?
Walter Roberson
Walter Roberson 2012년 4월 27일
The only way to know for sure is to try it.
I do not have access to MATLAB from home to experiment with. And even if I said that it worked, you would need to try it to be *certain*.
Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 30일
What about using 'ubit32=>float32' instead of '*float32'.....because binary is not in float, right?
Jan
Jan 2012년 4월 30일
'ubit32' is very inefficient, when it really splits the reading to 32 bits. 'uint32' reads and processes them in a block.

댓글을 달려면 로그인하십시오.

Dejan Cvijanovic
Dejan Cvijanovic 2012년 4월 26일

0 개 추천

Can anyone confirm correctness of last two posts?

댓글 수: 1

Jan
Jan 2012년 4월 30일
I'm sure *you* can confirm them. Simply compare the results and to be sure try it with a bunch of random numbers or bit-patterns.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2012년 4월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by