On Tuesday, 17 January 2023 at 21:50:06 UTC, matheus wrote:
>Have you compared the timings between this way (With ranges) and a normal way (Without ranges)?
Of course it is possible to speed it up. However, even as it is, it is enough to see the power of intervals. I would argue that you'll get fast results even with DMD Compiler!
import std.stdio;
import std.datetime.date;
void main()
{
import std.datetime.stopwatch : benchmark, StopWatch;
auto sw = StopWatch();
#line 1
sw.start();
enum xTest = 1200;
auto title = enumToStr!DayOfWeek(2);
auto date = Date(2023, 12, 1);
auto range = MyCalendar(date);
for(size_t test; test < xTest; range.popFront, ++test)
{
range.writeln; // month and year
title.writefln!"%-(%s %)"; // days of week
range.front.writeln; // formatted days
}
sw.stop();
writeln(sw.peek.total!"usecs");
}
struct MyCalendar
{
import std.array : appender, replicate;
import std.format : format, formattedWrite;
import std.string : capitalize;
Date date;
enum empty = false;
string front()
{
auto res = appender!string;
int day, dow = date.dayOfWeek;
// skip days:
res.formattedWrite("%s", " ".replicate(dow * 3));
for(day = 1; day <= date.daysInMonth; ++day)
{
res.formattedWrite("%2s ", day);
if(++dow % 7 == 0) res.put("\n");
}
if(dow % 7) res.put("\n");
return res.data;
}
void popFront()
{
date.roll!"months"(1);
}
string toString() const
{
const currentYear = date.year; // Current Year
const currentMonth = date.month; // Name of the month
const monthAndYear = format("%s/%s:",
currentMonth,
currentYear);
return capitalize(monthAndYear);
}
}
auto enumToStr(E)(size_t len = 0)
{
import std.conv : to;
string[] result;
for(E x = E.min; x <= E.max; x++)
{
result ~= len ? x.to!string[0..len]
: x.to!string;
}
return result;
}
SDB@79