Skip to content

Instantly share code, notes, and snippets.

@Tasssadar
Created Apr 18, 2020
Embed
What would you like to do?
#include <Arduino.h>
#include <FreeRTOS.h>
#include <sstream>
#include <mutex>
//static std::mutex m;
template <typename T>
void testPrintf(void *fmt)
{
const auto s1 = uxTaskGetStackHighWaterMark(NULL);
{
char buf[16];
snprintf(buf, sizeof(buf), (const char *)fmt, (T)xTaskGetTickCount());
}
const auto s2 = uxTaskGetStackHighWaterMark(NULL);
printf("printf %s: %d B\n", (const char *)fmt, s1 - s2);
while (1)
{
sleep(1);
}
vTaskDelete(NULL);
}
template <typename T>
void testStringStream(void *fmt)
{
const auto s1 = uxTaskGetStackHighWaterMark(NULL);
{
//m.lock();
std::ostringstream ss;
//m.unlock();
ss << (T)xTaskGetTickCount();
}
const auto s2 = uxTaskGetStackHighWaterMark(NULL);
printf("sstream %s: %d B\n", (const char *)fmt, s1 - s2);
vTaskDelete(NULL);
}
void setup()
{
// put your setup code here, to run once:
sleep(1);
xTaskCreate(testPrintf<int>, "", 4096, (void *)"%d", 1, NULL);
xTaskCreate(testPrintf<long long>, "", 4096, (void *)"%lld", 1, NULL);
xTaskCreate(testPrintf<double>, "", 4096, (void *)"%f", 1, NULL);
xTaskCreate(testPrintf<double>, "", 4096, (void *)"%.4f", 1, NULL);
xTaskCreate(testStringStream<int>, "", 4096, (void *)"%d", 1, NULL);
usleep(100000);
xTaskCreate(testStringStream<long long>, "", 4096, (void *)"%lld", 1, NULL);
usleep(100000);
xTaskCreate(testStringStream<double>, "", 4096, (void *)"%f", 1, NULL);
while (1)
sleep(1);
}
void loop()
{
// put your main code here, to run repeatedly:
}
/*
printf %d: 1072 B
printf %.4f: 1332 B
printf %lld: 928 B
printf %f: 1332 B
sstream %d: 948 B
sstream %lld: 1012 B
sstream %f: 2052 B
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment