These are the largely unedited results of a quick and dirty
benchmark I produced to determine whether string concatenation is
faster using the String.concat() method or a
StringBuffer/Builder. By inspecting the
source code, it is clear that the concat operation will be more
efficient for two strings, but it is not obvious that the concat
operation is faster for three.
I tested a few different techniques for concatenating strings, these were:
String.concat().StringBuffer.StringBuilder.StringBuffer that had been pre-set to the correct length.StringBuilder that had been pre-set to the correct length.Incidentally, the LK stands for "Length Known". Note that Java's
implicit String concatenation operator is not tested because the aim
of this benchmark is to assist in identfying the fastest way to
implement the operator. To identify the number of strings concatenated
by a particular test, a number is appended to the name given above. So
"buffer 2" means that two strings were concatenated using a
StringBuffer.
Each of the techiques was tested against a collection of strings of variable length with a pre-determined average (some techniques may work better than others on short or long strings). For more details of the implementation, just read the source code.
The test was run under Debian(4.0) Linux(2.6.15.1) on an Intel (P4@3.4Ghz) - all times quoted are in milliseconds. Java was executed without any command-line switches. Over an average of three runs, the results were as given below. The spreadsheet (OpenDocument format) used to record this data and generate the charts is available for download.
It appears (on this preliminary inspection at least) that the concat() method is fastest for combining 3 strings in all circumstances. Suprisingly, string concatenation appears to be slower in Java 1.6 than 1.5. These result may need to be verified.
| 1.6 | 250000 | 0 | 247 | 397 | 388 | 367 | 370 | 176 | 403 | 397 | 373 | 364 |
| 1.6 | 250000 | 10 | 203 | 423 | 420 | 404 | 399 | 409 | 767 | 766 | 420 | 426 |
| 1.6 | 250000 | 20 | 218 | 455 | 446 | 409 | 409 | 427 | 819 | 819 | 448 | 438 |
| 1.6 | 250000 | 30 | 216 | 490 | 491 | 439 | 427 | 449 | 855 | 854 | 488 | 472 |
| 1.6 | 250000 | 40 | 229 | 501 | 499 | 453 | 448 | 482 | 891 | 886 | 515 | 512 |
| 1.6 | 250000 | 50 | 242 | 520 | 523 | 458 | 472 | 511 | 938 | 926 | 537 | 539 |
| 1.6 | 250000 | 60 | 243 | 556 | 542 | 471 | 477 | 538 | 976 | 984 | 567 | 561 |
| 1.6 | 250000 | 70 | 248 | 580 | 574 | 491 | 503 | 555 | 999 | 996 | 589 | 589 |
| 1.6 | 250000 | 80 | 253 | 588 | 590 | 501 | 511 | 566 | 1047 | 1030 | 611 | 612 |
| 1.6 | 250000 | 90 | 266 | 607 | 604 | 521 | 514 | 589 | 1054 | 1051 | 636 | 627 |
| 1.6 | 250000 | 100 | 272 | 625 | 628 | 524 | 523 | 593 | 1094 | 1088 | 659 | 667 |
| java version | repetitions | avg length | concat 2 | buffer 2 | builder 2 | buffer 2 (LK) | builder 2 (LK) | concat 3 | buffer 3 | builder 3 | buffer 3 (LK) | builder 3 (LK) |
| 1.5 | 250000 | 0 | 185 | 375 | 368 | 360 | 357 | 181 | 369 | 354 | 358 | 347 |
| 1.5 | 250000 | 10 | 209 | 407 | 392 | 380 | 376 | 388 | 641 | 621 | 405 | 402 |
| 1.5 | 250000 | 20 | 216 | 423 | 414 | 391 | 384 | 404 | 679 | 672 | 433 | 419 |
| 1.5 | 250000 | 30 | 220 | 433 | 436 | 404 | 397 | 414 | 701 | 685 | 452 | 436 |
| 1.5 | 250000 | 40 | 245 | 453 | 450 | 410 | 392 | 429 | 735 | 720 | 460 | 447 |
| 1.5 | 250000 | 50 | 231 | 469 | 464 | 433 | 427 | 442 | 757 | 751 | 492 | 476 |
| 1.5 | 250000 | 60 | 237 | 476 | 473 | 422 | 425 | 473 | 806 | 778 | 501 | 493 |
| 1.5 | 250000 | 70 | 259 | 492 | 481 | 429 | 423 | 469 | 810 | 794 | 522 | 494 |
| 1.5 | 250000 | 80 | 249 | 510 | 504 | 435 | 430 | 481 | 838 | 823 | 526 | 522 |
| 1.5 | 250000 | 90 | 262 | 521 | 519 | 454 | 460 | 493 | 860 | 860 | 541 | 538 |
| 1.5 | 250000 | 100 | 255 | 536 | 533 | 457 | 455 | 516 | 893 | 881 | 561 | 554 |