Considering the suggestion of using modern string arrays instead of char vectors proposed by Cris Luengo, I am trying to make a code snippet for performance comparison of these two cases.
The experimental implementation
%% Print system information
system('systeminfo');
%% Setup: test parameters
concatTestTimes = 25;
repetitionTimes = 10;
fprintf("The performance test will run %d times and the count of the concatenation operation in each test iteration is %d.\n", repetitionTimes, concatTestTimes);
%% Setup: preparing the space for storing evaluation results
resultsOfCharVectors = zeros(1, repetitionTimes);
resultsOfString = zeros(1, repetitionTimes);
%% Run tests
for eachIterate = 1:repetitionTimes
tic;
initialCharVectors = ['123'];
for i = 1:concatTestTimes
initialCharVectors = [initialCharVectors filesep initialCharVectors];
end
resultsOfCharVectors(eachIterate) = toc;
tic;
initialString = "123";
for i = 1:concatTestTimes
initialString = initialString + convertCharsToStrings(filesep) + initialString;
end
resultsOfString(eachIterate) = toc;
end
%% Print test results
fprintf("The average execution time of char vectors: %d.\n", mean(resultsOfCharVectors, 'all'));
fprintf("The average execution time of string: %d.\n", mean(resultsOfString, 'all'));
%% Save results (to Excel file)
outputFolderRoot = '.';
outputFilename = fullfile(outputFolderRoot, 'StringAndCharVecConcatComparisonResults.xlsx');
% Write title
outputTitle = ["Test Iteration" "Performance of using CharVectors" "Performance of using String"];
writematrix(outputTitle, outputFilename, 'Sheet', 1, 'Range', 'A1');
indexColumn = 1:repetitionTimes;
outputData = [indexColumn' resultsOfCharVectors' resultsOfString'];
writematrix(outputData, outputFilename, 'Sheet', 1, 'Range', 'A2');
%% Plot results
figure('Renderer', 'painters', 'Position', [10 10 900 600]);
plot(resultsOfCharVectors);
hold;
plot(resultsOfString);
xlabel('Test Iteration');
ylabel('Execution Time (seconds)');
title('String / Char Vector Concatenation Performance Comparison');
legend('Performance of using CharVectors','Performance of using String');
grid on;
%% Save results
saveas(gcf,'StringAndCharVecConcatComparison.png');
Output of the Proposed Experimental Implementation
Host Name: DESKTOP-DFPCSK8
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.19043 N/A Build 19043
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner: user
Registered Organization:
Product ID: 00330-80000-00000-AA846
Original Install Date: 5/24/2022, 4:44:08 AM
System Boot Time: 6/7/2022, 1:53:46 PM
System Manufacturer: Gigabyte Technology Co., Ltd.
System Model: Z490 AORUS MASTER
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 165 Stepping 5 GenuineIntel ~2904 Mhz
BIOS Version: American Megatrends Inc. F7, 10/27/2020
Windows Directory: C:\Windows
System Directory: C:\Windows\system32
Boot Device: \Device\HarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC+08:00) Taipei
Total Physical Memory: 65,460 MB
Available Physical Memory: 44,378 MB
Virtual Memory: Max Size: 94,520 MB
Virtual Memory: Available: 40,532 MB
Virtual Memory: In Use: 53,988 MB
Page File Location(s): C:\pagefile.sys
Domain: WORKGROUP
Logon Server: \\DESKTOP-DFPCSK8
Hotfix(s): 6 Hotfix(s) Installed.
[01]: KB5013887
[02]: KB5000736
[03]: KB5005716
[04]: KB5014023
[05]: KB5014035
[06]: KB5001405
Network Card(s): 3 NIC(s) Installed.
[01]: Intel(R) Wi-Fi 6 AX201 160MHz
Connection Name: Wi-Fi
DHCP Enabled: Yes
DHCP Server: 1.1.1.1
IP address(es)
[01]: 10.174.192.53
[02]: fe80::6de6:48:1954:9405
[02]: Intel(R) Ethernet Controller I225-V
Connection Name: Ethernet
Status: Media disconnected
[03]: Bluetooth Device (Personal Area Network)
Connection Name: Bluetooth Network Connection
Status: Media disconnected
Hyper-V Requirements: VM Monitor Mode Extensions: Yes
Virtualization Enabled In Firmware: Yes
Second Level Address Translation: Yes
Data Execution Prevention Available: Yes
The performance test will run 10 times and the count of the concatenation operation in each test iteration is 25.
The average execution time of char vectors: 1.902807e-01.
The average execution time of string: 2.932196e-01.
Current plot held
About the evaluation results, I found that the performance of using char vectors is slightly better than the case of using string when it comes to the concatenation task. However, I am wondering is the proposed experimental implementation a good way to performance comparison task. Is there any defect or any possible improvement to this kind of task?
All suggestions are welcome.