1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-11-23 17:30:17 +01:00

Benchmark::OnRun: clarify, simplify, avoid possible divide by 0

This commit is contained in:
Paul Licameli
2017-02-22 23:37:28 -05:00
parent 6a57987f88
commit 40651241d9

View File

@@ -354,12 +354,12 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
srand(randSeed);
int nChunks, chunkSize;
size_t nChunks, chunkSize;
//chunkSize = 7500 + (rand() % 1000);
chunkSize = 200 + (rand() % 100);
nChunks = (dataSize * 1048576) / (chunkSize*sizeof(short));
while(nChunks < 20 || chunkSize > (blockSize*1024)/4) {
chunkSize = (chunkSize / 2) + (rand() % 100);
while(nChunks < 20 || chunkSize > ((unsigned long)(blockSize)*1024)/4) {
chunkSize = std::max( size_t(1), (chunkSize / 2) + (rand() % 100) );
nChunks = (dataSize * 1048576) / (chunkSize*sizeof(short));
}
@@ -374,7 +374,6 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
int trials = numEdits;
short *small1 = new short[nChunks];
short *small2 = new short[nChunks];
short *block = new short[chunkSize];
Printf(wxT("Preparing...\n"));
@@ -416,8 +415,13 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
timer.Start();
for (z = 0; z < trials; z++) {
int x0 = rand() % nChunks;
int xlen = 1 + (rand() % (nChunks - x0));
// First chunk to cut
// 0 <= x0 < nChunks
const size_t x0 = rand() % nChunks;
// Number of chunks to cut
// 1 <= xlen <= nChunks - x0
const size_t xlen = 1 + (rand() % (nChunks - x0));
if (mEditDetail)
Printf(wxT("Cut: %d - %d \n"), x0 * chunkSize, (x0 + xlen) * chunkSize);
@@ -431,7 +435,10 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
goto fail;
}
int y0 = rand() % (nChunks - xlen);
// Position to paste
// 0 <= y0 <= nChunks - xlen
const size_t y0 = rand() % (nChunks - xlen + 1);
if (mEditDetail)
Printf(wxT("Paste: %d\n"), y0 * chunkSize);
@@ -447,18 +454,12 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
t->GetClipByIndex(0)->GetSequence()->GetNumSamples().as_long_long());
goto fail;
}
// Copy
for (i = 0; i < xlen; i++)
small2[i] = small1[x0 + i];
// Delete
for (i = 0; i < (nChunks - x0 - xlen); i++)
small1[x0 + i] = small1[x0 + xlen + i];
// Insert
for (i = 0; i < (nChunks - xlen - y0); i++)
small1[nChunks - i - 1] = small1[nChunks - i - 1 - xlen];
// Paste
for (i = 0; i < xlen; i++)
small1[y0 + i] = small2[i];
// Permute small1 correspondingly to the cut and paste
auto first = &small1[0];
if (x0 + xlen < nChunks)
std::rotate( first + x0, first + x0 + xlen, first + nChunks );
std::rotate( first + y0, first + nChunks - xlen, first + nChunks );
}
elapsed = timer.Time();
@@ -534,7 +535,6 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
success:
delete[]small1;
delete[]small2;
delete[]block;
dd.reset();