473,804 Members | 3,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float algorithm is slow

float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?

Thanks,

Wenfei

Nov 15 '05 #1
19 2650
Wenfei wrote:
float percentage;

for (j = 0; j < 10000000; j++) {
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time? cat main.c #include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[]) {

const
size_t n = 10000000;
float buffer[n];
size_t totalBytes = 0;
const
float_t frequency = 1.0;
const
float_t sampleFreq = 1.0;
const
float_t pi = 3.1415926535897 9323846;
const
float_t volume = 1.0;

for (size_t j = 0; j < n; ++j) {
float_t percentage = sinf(frequency* j*2*pi/sampleFreq);
buffer[totalBytes] =ceilf(volume*p ercentage) + volume;
totalBytes++;
}

return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -o main main.c -lm
time ./main

3.694u 0.258s 0:03.92 100.5% 0+0k 0+0io 0pf+0w
Nov 15 '05 #2
Wenfei wrote:
float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


Yes: Change the iteration count from 10000000 to 0, and
the code will almost certainly run faster.

In other words, micro-benchmarks of this sort are not
very informative. What are you really trying to do?

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
>float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable,
My guess is that if you GOT RID OF the float variable, it would
take about the same time:
for (j = 0; j < 10000000; j++) {
buffer[totalBytes] =ceilf(volume * sinf(frequency * j * 2 * 3.14159 / sampleFreq )) + volume;
totalBytes++;
}
the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


You haven't demonstrated why taking two seconds is a problem yet.
Cut down the number of iterations? Get a faster machine?
Doing the calculation in double might make it faster (although on
Intel *86 it probably won't).

Gordon L. Burditt
Nov 15 '05 #4
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
Wenfei <ye*******@hotm ail.com> wrote:


float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


* Use a small table of sine values instead of sinf() function.
It appears that you're downsampling to the width of a char
anyway, so table of somewhat more than 256 sine values probably
won't make things much worse.

* Or just map out one complete waveform and just repeatedly copy that
throughout the rest of the buffer.

* Or Just make one copy of the waveform and index into that as
appropriate when you need the results.
--
7842++
Nov 15 '05 #5
Wenfei wrote:
float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


<OT>
It is unlikely that using double will be any faster.

On my system, 66% of the time is spent in the first statement inside
the loop, 27% on the second. The sinf and ceilf function calls are by
far the most intensive parts of these statements. If your system is
similiar, you might try replacing the sinf call with a lookup table of
precalculated values with a tradeoff of accuracy and memory usage. You
could also use a macro of inline function in place of ceilf.

You didn't specify how much faster you need it, what tradeoffs are
feasible, what you have tried already, or even what exactly you are
trying to accomplish. Perhaps the algorithm itself could be improved
but it is difficult to attempt to do so without knowing what the
specifications are.
</OT>

Robert Gamble

Nov 15 '05 #6
Wenfei wrote:

float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


I assume your linux machine has an x86 based processor. If thats the
case then you problem is probably the ceilf() function. The implementation
of that function requires that the FPU's control word to be modified at
the start of ceilf() and restored afterwards. Every time the FPU control
word is modified, it causes a stall in the FPU pipeline.

You might try replacing ceilf() with the C99 function lrintf().
Unfortunately lrintf() is a round function, not a ceil function so you
might need to replace

ceilf (x)

with

lrintf (x + 0.5).
Also have a look at this paper:

http://www.mega-nerd.com/FPcast/

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"It has been discovered that C++ provides a remarkable facility
for concealing the trival details of a program -- such as where
its bugs are." -- David Keppel
Nov 15 '05 #7
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
Wenfei <ye*******@hotm ail.com> wrote:

buffer[totalBytes] =ceilf(volume * percentage) + volume;


Sorry for posting twice to the same thread, but ceilf() may not be
strictly really necessary. If buffer is an array of some kind of
integer type then the assignment is going to do a conversion from float
to int anyway. Unless you have some mathematically rigorous standard
to uphold, you might get a "close enough" result by rounding up. E.g.

buffer[totalBytes] = (volume * percentage + 0.5) + volume;
--
7842++
Nov 15 '05 #8
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
"Wenfei" <ye*******@hotm ail.com> wrote:
float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?


I wouldn't worry about the speed, because your results are garbage
anyway.

When j has the maximum value 1e7 - 1, what is the difference between j *
2 * 3.14159 and j * 2 * pi?

Once you've fixed this, just a hint (mathematics is fun): Consecutive
terms of any function of the form

f (n) = a * sin (x * n + b) + c * cos (x * n + d)

can be calculated using one single multiplication and one single
addition.
Nov 15 '05 #9
On Tue, 05 Jul 2005 22:57:23 +0100, Christian Bau wrote:

....
Once you've fixed this, just a hint (mathematics is fun): Consecutive
terms of any function of the form

f (n) = a * sin (x * n + b) + c * cos (x * n + d)

can be calculated using one single multiplication and one single
addition.


But you need to be careful about accumulation of errors.

Lawrence

Nov 15 '05 #10

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

Similar topics

11
548
by: Wenfei | last post by:
float percentage; for (j = 0; j < 10000000; j++) { percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq ); buffer =ceilf(volume * percentage) + volume; totalBytes++; } Because the float variable, the above loop take 2 seconds in c or c++
54
8383
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
13
2726
by: Michele Guidolin | last post by:
Hello to everybody. I'm doing some benchmark about a red black Gauss Seidel algorithm with 2 dimensional grid of different size and type, I have some strange result when I change the computation from double to float. Here are the time of test with different grid SIZE and type: SIZE 128 256 512
2
4289
by: Stu Smith | last post by:
A feature we'd like to see in VS 2004 is a compiler warning for equality comparisons on floating values. Why? Because the behaviour allowed under the ECMA spec is somewhat suprising. (Well it suprised us). If you run the program below in debug mode (F5), it prints True. If you run it in non-debug mode (Ctrl-F5), you get False. We had a look at the ECMA spec (CLI - 12.1.3 line 35) and this behaviour seems perfectly acceptable.
4
2252
by: zing | last post by:
Our company is in the startup phase of a large project involving lots of network traffic. At this point, I'm trying to find out whether TCP will be fast enough for the task. I've read a few articles that promote UDP, claiming that TCP is slow, mainly written by gamers. But I've also read some articles by more scientific sources, which made it clear that a lot of progress has been made during the last 15 years or so. I actually find it...
116
36009
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
0
2973
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm so interest about this encoding algorithms. because our group is going to do a project under the ITU-T Recommendation X.891. this is named as fast infoset. this is an open source project in c language. there is an implementation in java which by...
3
10671
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience with this and if there is a better solution. -jeff
2
5480
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure...
0
9706
marktang
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...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
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,...
1
7620
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
6853
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
5522
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...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.