質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -197,3 +197,517 @@
|
|
197
197
|
CPU : AMD A4-5000 APU with Radeon(TM) HD Graphics
|
198
198
|
|
199
199
|
gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
<2017/1/23 追記>
|
206
|
+
|
207
|
+
皆さん回答ありがとうございました!ikedasさんのものとほとんど変わりませんが、皆さんが回答してくださったものを一通り僕の環境でも試してみました。与えられる整数値の桁数の範囲が分かっているなら、2分探索が一番良さそうです。とても勉強になりました、みなさん本当にありがとうございました!以下修正版と結果です。
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
```C
|
212
|
+
|
213
|
+
#include <stdio.h>
|
214
|
+
|
215
|
+
#include <math.h>
|
216
|
+
|
217
|
+
#include <time.h>
|
218
|
+
|
219
|
+
#include <limits.h>
|
220
|
+
|
221
|
+
#include <assert.h>
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
#define LOOP 10000000
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
void countDigit(int n)
|
232
|
+
|
233
|
+
{
|
234
|
+
|
235
|
+
int digit = 1;
|
236
|
+
|
237
|
+
clock_t start,end;
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
start = clock();
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
for(size_t i = 0;i < LOOP; ++i) {
|
246
|
+
|
247
|
+
while((n /= 10) != 0)
|
248
|
+
|
249
|
+
digit++;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
end = clock();
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
printf("%-25s\tdigit::%d\ttime::%fs\n",
|
260
|
+
|
261
|
+
"countDigit()",digit,((double)end - start)/CLOCKS_PER_SEC);
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
void countDigitUsingLog(int n)
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
clock_t start,end;
|
272
|
+
|
273
|
+
int digit;
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
start = clock();
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
for(size_t i = 0;i < LOOP; ++i)
|
282
|
+
|
283
|
+
digit = log10(n) + 1;
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
end = clock();
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
printf("%-25s\tdigit::%d\ttime::%fs\n",
|
292
|
+
|
293
|
+
"countDigitUsingLog()",digit,((double)end - start)/CLOCKS_PER_SEC);
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
void countDigitBinary(int n)
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
int digit;
|
304
|
+
|
305
|
+
clock_t start,end;
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
start = clock();
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
for(size_t i = 0;i < LOOP; ++i) {
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
if (n < 100000) {
|
318
|
+
|
319
|
+
if (n < 1000) {
|
320
|
+
|
321
|
+
if (n < 10)
|
322
|
+
|
323
|
+
digit = 1;
|
324
|
+
|
325
|
+
else if (n < 100)
|
326
|
+
|
327
|
+
digit = 2;
|
328
|
+
|
329
|
+
else
|
330
|
+
|
331
|
+
digit = 3;
|
332
|
+
|
333
|
+
} else {
|
334
|
+
|
335
|
+
if (n < 10000)
|
336
|
+
|
337
|
+
digit = 4;
|
338
|
+
|
339
|
+
else
|
340
|
+
|
341
|
+
digit = 5;
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
} else {
|
346
|
+
|
347
|
+
if (n < 10000000) {
|
348
|
+
|
349
|
+
if (n < 1000000)
|
350
|
+
|
351
|
+
digit = 6;
|
352
|
+
|
353
|
+
else
|
354
|
+
|
355
|
+
digit = 7;
|
356
|
+
|
357
|
+
} else {
|
358
|
+
|
359
|
+
if (n < 100000000)
|
360
|
+
|
361
|
+
digit = 8;
|
362
|
+
|
363
|
+
else if (n < 1000000000)
|
364
|
+
|
365
|
+
digit = 9;
|
366
|
+
|
367
|
+
else
|
368
|
+
|
369
|
+
digit = 10;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
end = clock();
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
printf("%-25s\tdigit::%d\ttime::%fs\n",
|
386
|
+
|
387
|
+
"countDigitBinary()",digit,((double)end - start)/CLOCKS_PER_SEC);
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
int digitPoor(int n)
|
394
|
+
|
395
|
+
{
|
396
|
+
|
397
|
+
assert(INT_MAX == 2147483647);
|
398
|
+
|
399
|
+
assert(n >= 0);
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
if (n < 10) return 1;
|
404
|
+
|
405
|
+
if (n < 100) return 2;
|
406
|
+
|
407
|
+
if (n < 1000) return 3;
|
408
|
+
|
409
|
+
if (n < 10000) return 4;
|
410
|
+
|
411
|
+
if (n < 100000) return 5;
|
412
|
+
|
413
|
+
if (n < 1000000) return 6;
|
414
|
+
|
415
|
+
if (n < 10000000) return 7;
|
416
|
+
|
417
|
+
if (n < 100000000) return 8;
|
418
|
+
|
419
|
+
if (n < 1000000000) return 9;
|
420
|
+
|
421
|
+
return 10;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
void countDigitPoor(int n)
|
428
|
+
|
429
|
+
{
|
430
|
+
|
431
|
+
clock_t start,end;
|
432
|
+
|
433
|
+
int digit;
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
start = clock();
|
438
|
+
|
439
|
+
for (size_t i=0; i < 10000000; ++i) {
|
440
|
+
|
441
|
+
digit = digitPoor(n);
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
end = clock();
|
446
|
+
|
447
|
+
printf("%-25s\tdigit::%d\ttime::%fs\n",
|
448
|
+
|
449
|
+
"countDigitPoor()",digit,((double)end - start)/CLOCKS_PER_SEC);
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
void countDigitUsingSprintf(int n)
|
456
|
+
|
457
|
+
{
|
458
|
+
|
459
|
+
clock_t start,end;
|
460
|
+
|
461
|
+
int digit;
|
462
|
+
|
463
|
+
char dummy[10];
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
start = clock();
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
for(size_t i = 0;i < LOOP; ++i)
|
472
|
+
|
473
|
+
digit = sprintf(dummy, "%d", n);
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
end = clock();
|
478
|
+
|
479
|
+
printf("%-25s\tdigit::%d\ttime::%fs\n",
|
480
|
+
|
481
|
+
"countDigitUsingSprintf()",digit,((double)end - start)/CLOCKS_PER_SEC);
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
void countForLoop()
|
488
|
+
|
489
|
+
{
|
490
|
+
|
491
|
+
clock_t start,end;
|
492
|
+
|
493
|
+
volatile int x=1;
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
start = clock();
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
for (size_t i = 0; i < LOOP; ++i)
|
502
|
+
|
503
|
+
{
|
504
|
+
|
505
|
+
x=i;
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
end = clock();
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
printf("%-25s\t--------\ttime::%fs\n",
|
516
|
+
|
517
|
+
"countForLoop()",((double)end - start)/CLOCKS_PER_SEC);
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
int main(void)
|
524
|
+
|
525
|
+
{
|
526
|
+
|
527
|
+
int n = 0;
|
528
|
+
|
529
|
+
int i;
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
for(i = 1;i < 10;i++) {
|
534
|
+
|
535
|
+
n = n * 10 + i;
|
536
|
+
|
537
|
+
printf("N = %d\n",n);
|
538
|
+
|
539
|
+
countForLoop();
|
540
|
+
|
541
|
+
countDigit(n);
|
542
|
+
|
543
|
+
countDigitUsingLog(n);
|
544
|
+
|
545
|
+
countDigitBinary(n);
|
546
|
+
|
547
|
+
countDigitPoor(n);
|
548
|
+
|
549
|
+
countDigitUsingSprintf(n);
|
550
|
+
|
551
|
+
printf("\n");
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
return 0;
|
558
|
+
|
559
|
+
}
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
```
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
```ここに言語を入力
|
568
|
+
|
569
|
+
N = 1
|
570
|
+
|
571
|
+
countForLoop() -------- time::0.045770s
|
572
|
+
|
573
|
+
countDigit() digit::1 time::0.100439s
|
574
|
+
|
575
|
+
countDigitUsingLog() digit::1 time::0.378452s
|
576
|
+
|
577
|
+
countDigitBinary() digit::1 time::0.053568s
|
578
|
+
|
579
|
+
countDigitPoor() digit::1 time::0.079120s
|
580
|
+
|
581
|
+
countDigitUsingSprintf() digit::1 time::2.265236s
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
N = 12
|
586
|
+
|
587
|
+
countForLoop() -------- time::0.043182s
|
588
|
+
|
589
|
+
countDigit() digit::2 time::0.100408s
|
590
|
+
|
591
|
+
countDigitUsingLog() digit::2 time::1.012793s
|
592
|
+
|
593
|
+
countDigitBinary() digit::2 time::0.060286s
|
594
|
+
|
595
|
+
countDigitPoor() digit::2 time::0.092724s
|
596
|
+
|
597
|
+
countDigitUsingSprintf() digit::2 time::2.341804s
|
598
|
+
|
599
|
+
|
600
|
+
|
601
|
+
N = 123
|
602
|
+
|
603
|
+
countForLoop() -------- time::0.042746s
|
604
|
+
|
605
|
+
countDigit() digit::3 time::0.100416s
|
606
|
+
|
607
|
+
countDigitUsingLog() digit::3 time::1.004158s
|
608
|
+
|
609
|
+
countDigitBinary() digit::3 time::0.060143s
|
610
|
+
|
611
|
+
countDigitPoor() digit::3 time::0.108729s
|
612
|
+
|
613
|
+
countDigitUsingSprintf() digit::3 time::2.442325s
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
N = 1234
|
618
|
+
|
619
|
+
countForLoop() -------- time::0.045008s
|
620
|
+
|
621
|
+
countDigit() digit::4 time::0.100400s
|
622
|
+
|
623
|
+
countDigitUsingLog() digit::4 time::1.004526s
|
624
|
+
|
625
|
+
countDigitBinary() digit::4 time::0.048930s
|
626
|
+
|
627
|
+
countDigitPoor() digit::4 time::0.132441s
|
628
|
+
|
629
|
+
countDigitUsingSprintf() digit::4 time::2.530141s
|
630
|
+
|
631
|
+
|
632
|
+
|
633
|
+
N = 12345
|
634
|
+
|
635
|
+
countForLoop() -------- time::0.042238s
|
636
|
+
|
637
|
+
countDigit() digit::5 time::0.100400s
|
638
|
+
|
639
|
+
countDigitUsingLog() digit::5 time::1.008050s
|
640
|
+
|
641
|
+
countDigitBinary() digit::5 time::0.060654s
|
642
|
+
|
643
|
+
countDigitPoor() digit::5 time::0.144062s
|
644
|
+
|
645
|
+
countDigitUsingSprintf() digit::5 time::2.624742s
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
N = 123456
|
650
|
+
|
651
|
+
countForLoop() -------- time::0.042451s
|
652
|
+
|
653
|
+
countDigit() digit::6 time::0.100724s
|
654
|
+
|
655
|
+
countDigitUsingLog() digit::6 time::1.008027s
|
656
|
+
|
657
|
+
countDigitBinary() digit::6 time::0.058717s
|
658
|
+
|
659
|
+
countDigitPoor() digit::6 time::0.177779s
|
660
|
+
|
661
|
+
countDigitUsingSprintf() digit::6 time::2.751899s
|
662
|
+
|
663
|
+
|
664
|
+
|
665
|
+
N = 1234567
|
666
|
+
|
667
|
+
countForLoop() -------- time::0.041235s
|
668
|
+
|
669
|
+
countDigit() digit::7 time::0.100376s
|
670
|
+
|
671
|
+
countDigitUsingLog() digit::7 time::1.004352s
|
672
|
+
|
673
|
+
countDigitBinary() digit::7 time::0.069315s
|
674
|
+
|
675
|
+
countDigitPoor() digit::7 time::0.193293s
|
676
|
+
|
677
|
+
countDigitUsingSprintf() digit::7 time::2.825540s
|
678
|
+
|
679
|
+
|
680
|
+
|
681
|
+
N = 12345678
|
682
|
+
|
683
|
+
countForLoop() -------- time::0.041624s
|
684
|
+
|
685
|
+
countDigit() digit::8 time::0.100474s
|
686
|
+
|
687
|
+
countDigitUsingLog() digit::8 time::1.004698s
|
688
|
+
|
689
|
+
countDigitBinary() digit::8 time::0.075757s
|
690
|
+
|
691
|
+
countDigitPoor() digit::8 time::0.215140s
|
692
|
+
|
693
|
+
countDigitUsingSprintf() digit::8 time::2.937961s
|
694
|
+
|
695
|
+
|
696
|
+
|
697
|
+
N = 123456789
|
698
|
+
|
699
|
+
countForLoop() -------- time::0.043923s
|
700
|
+
|
701
|
+
countDigit() digit::9 time::0.100387s
|
702
|
+
|
703
|
+
countDigitUsingLog() digit::9 time::1.004096s
|
704
|
+
|
705
|
+
countDigitBinary() digit::9 time::0.094569s
|
706
|
+
|
707
|
+
countDigitPoor() digit::9 time::0.254226s
|
708
|
+
|
709
|
+
countDigitUsingSprintf() digit::9 time::3.033319s
|
710
|
+
|
711
|
+
|
712
|
+
|
713
|
+
```
|