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