本文整理汇总了C++中Queue::EnsureSize方法的典型用法代码示例。如果您正苦于以下问题:C++ Queue::EnsureSize方法的具体用法?C++ Queue::EnsureSize怎么用?C++ Queue::EnsureSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Queue
的用法示例。
在下文中一共展示了Queue::EnsureSize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
for (int j=0; j<testSize; j++) printf("Now item %i = %i\n", j, q[j]);
}
printf("SORT TEST 2\n");
{
Queue<String> q2;
for (int i=0; i<testSize; i++)
{
int next = rand()%255;
char buf[64];
sprintf(buf, "%i", next);
TEST(q2.AddTail(buf));
printf("Added item %i = %s\n", i, q2[i].Cstr());
}
printf("sorting strings...\n");
q2.Sort();
for (int j=0; j<testSize; j++) printf("Now item %i = %s\n", j, q2[j].Cstr());
}
printf("REMOVE DUPLICATES test\n");
{
Queue<int> q;
const int vars[] = {9,2,3,5,8,3,5,6,6,7,2,3,4,6,8,9,3,5,6,4,3,2,1};
if (q.AddTailMulti(vars, ARRAYITEMS(vars)) == B_NO_ERROR)
{
q.RemoveDuplicateItems();
for (uint32 i=0; i<q.GetNumItems(); i++) printf("%u ", q[i]); printf("\n");
}
}
{
const uint32 NUM_ITEMS = 1000000;
const uint32 NUM_RUNS = 3;
Queue<int> q; (void) q.EnsureSize(NUM_ITEMS, true);
double tally = 0.0;
for (uint32 t=0; t<NUM_RUNS; t++)
{
printf("SORT SPEED TEST ROUND " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC":\n", t+1, NUM_RUNS);
srand(0); for (uint32 i=0; i<NUM_ITEMS; i++) q[i] = rand(); // we want this to be repeatable, hence srand(0)
uint64 startTime = GetRunTime64();
q.Sort();
uint64 elapsed = (GetRunTime64()-startTime);
double itemsPerSecond = ((double)NUM_ITEMS*((double)MICROS_PER_SECOND))/(elapsed);
printf(" It took " UINT64_FORMAT_SPEC" microseconds to sort " UINT32_FORMAT_SPEC" items, so we sorted %f items per second\n", elapsed, NUM_ITEMS, itemsPerSecond);
tally += itemsPerSecond;
}
printf("GRAND AVERAGE ITEMS PER SECOND WAS %f items per second\n", tally/NUM_RUNS);
}
PrintAndClearStringCopyCounts("Before String Sort Tests");
{
const uint32 NUM_ITEMS = 1000000;
const uint32 NUM_RUNS = 3;
Queue<String> q; (void) q.EnsureSize(NUM_ITEMS, true);
double tally = 0.0;
for (uint32 t=0; t<NUM_RUNS; t++)
{
printf("STRING SORT SPEED TEST ROUND " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC":\n", t+1, NUM_RUNS);
srand(0); for (uint32 i=0; i<NUM_ITEMS; i++) q[i] = String("FooBarBaz-%1").Arg(rand()).Pad(500); // we want this to be repeatable, hence srand(0)
uint64 startTime = GetRunTime64();
q.Sort();
示例2: main
// This program tests the relative speeds of various object allocation strategies.
int main(int argc, char ** argv)
{
CompleteSetupSystem css; // required!
const uint32 NUM_OBJECTS = 10000000;
Queue<MessageRef> tempQ;
if (tempQ.EnsureSize(NUM_OBJECTS, true) != B_NO_ERROR) return 10;
int whichTest = (argc>1) ? atoi(argv[1]) : -1;
uint64 startTime = GetRunTime64();
switch(whichTest)
{
case 1:
{
// See how long it takes to allocate an array of objects
(void) new Message[NUM_OBJECTS];
}
break;
case 2:
{
// As above, but with deletion also
delete [] new Message[NUM_OBJECTS];
}
break;
case 3:
{
// See how long it takes to allocate each object individually
for (uint32 i=0; i<NUM_OBJECTS; i++) tempQ[i].SetRef(new Message);
}
break;
case 4:
{
// As above, but we delete the item after allocating it
for (uint32 i=0; i<NUM_OBJECTS; i++) tempQ[i].SetRef(new Message);
for (uint32 i=0; i<NUM_OBJECTS; i++) tempQ[i].Reset();
}
break;
case 5:
{
// See how long it takes to allocate each object individually
for (uint32 i=0; i<NUM_OBJECTS; i++) tempQ[i] = GetMessageFromPool();
}
break;
case 6:
{
// As above, but then we clear the queue
for (uint32 i=0; i<NUM_OBJECTS; i++) tempQ[i] = GetMessageFromPool();
tempQ.Clear();
}
break;
case 7:
{
// As above, but we only use one object at a time
for (uint32 i=0; i<NUM_OBJECTS; i++) (void) GetMessageFromPool();
}
break;
default:
printf("Usage: testpools <testnum> (where testnum is between 1 and 6)\n");
break;
}
uint64 endTime = GetRunTime64();
printf("Test duration for " UINT32_FORMAT_SPEC " objects was " UINT64_FORMAT_SPEC "ms \n", NUM_OBJECTS, (endTime-startTime)/1000);
if ((argc > 2)&&(strcmp(argv[2], "hold") == 0))
{
printf("Holding indefinitely, so that you can look at OS reported memory usage...\n");
while(1) Snooze64(SecondsToMicros(10));
}
return 0;
}
示例3: main
// This program tests the SocketMultiplexer class by seeing how many chained socket-pairs
// we can chain a message through sequentially
int main(int argc, char ** argv)
{
CompleteSetupSystem css;
uint32 numPairs = 5;
if (argc > 1) numPairs = atoi(argv[1]);
bool quiet = false;
if ((argc > 2)&&(strcmp(argv[2], "quiet") == 0)) quiet = true;
#ifdef __APPLE__
// Tell MacOS/X that yes, we really do want to create this many file descriptors
struct rlimit rl;
rl.rlim_cur = rl.rlim_max = (numPairs*2)+5;
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) perror("setrlimit");
#endif
printf("Testing %i socket-pairs chained together...\n", numPairs);
Queue<ConstSocketRef> senders; (void) senders.EnsureSize(numPairs, true);
Queue<ConstSocketRef> receivers; (void) receivers.EnsureSize(numPairs, true);
for (uint32 i=0; i<numPairs; i++)
{
if (CreateConnectedSocketPair(senders[i], receivers[i]) != B_NO_ERROR)
{
printf("Error, failed to create socket pair #" UINT32_FORMAT_SPEC "!\n", i);
return 10;
}
}
// Start the game off
char c = 'C';
if (SendData(senders[0], &c, 1, false) != 1)
{
printf("Error, couldn't send initial byte!\n");
return 10;
}
uint64 count = 0;
uint64 tally = 0;
uint64 minRunTime = (uint64)-1;
uint64 maxRunTime = 0;
SocketMultiplexer multiplexer;
uint64 endTime = GetRunTime64() + SecondsToMicros(10);
bool error = false;
while(error==false)
{
for (uint32 i=0; i<numPairs; i++)
{
if (multiplexer.RegisterSocketForReadReady(receivers[i].GetFileDescriptor()) != B_NO_ERROR)
{
printf("Error, RegisterSocketForRead() failed for receiver #" UINT32_FORMAT_SPEC "!\n", i);
error = true;
break;
}
}
if (error) break;
uint64 then = GetRunTime64();
if (then >= endTime) break;
int ret = multiplexer.WaitForEvents();
if (ret < 0)
{
printf("WaitForEvents errored out, aborting test!\n");
break;
}
uint64 elapsed = GetRunTime64()-then;
if (quiet == false) printf("WaitForEvents returned %i after " UINT64_FORMAT_SPEC " microseconds.\n", ret, elapsed);
count++;
tally += elapsed;
minRunTime = muscleMin(minRunTime, elapsed);
maxRunTime = muscleMax(maxRunTime, elapsed);
for (uint32 i=0; i<numPairs; i++)
{
if (multiplexer.IsSocketReadyForRead(receivers[i].GetFileDescriptor()))
{
char buf[64];
int32 numBytesReceived = ReceiveData(receivers[i], buf, sizeof(buf), false);
if (quiet == false) printf("Receiver #" UINT32_FORMAT_SPEC " signalled ready-for-read, read " INT32_FORMAT_SPEC " bytes.\n", i, numBytesReceived);
if (numBytesReceived > 0)
{
uint32 nextIdx = (i+1)%numPairs;
int32 sentBytes = SendData(senders[nextIdx], buf, numBytesReceived, false);
if (quiet == false) printf("Sent " INT32_FORMAT_SPEC " bytes on sender #" UINT32_FORMAT_SPEC "\n", sentBytes, nextIdx);
}
}
}
}
printf("Test complete: WaitEvents() called " UINT64_FORMAT_SPEC " times, averageTime=" UINT64_FORMAT_SPEC "uS, minimumTime=" UINT64_FORMAT_SPEC "uS, maximumTime=" UINT64_FORMAT_SPEC "uS.\n", count, tally/(count?count:1), minRunTime, maxRunTime);
return 0;
}