Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Convert make intervals to select using array slices #19

Open
evelynhathaway opened this issue Sep 4, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@evelynhathaway
Copy link
Owner

evelynhathaway commented Sep 4, 2023

Instead of pushing pixels repeatedly, I wonder if this would be faster: only track an index for the duration of an interval, and then once it no longer matches, push a slice that represents the interval.

@evelynhathaway evelynhathaway added the enhancement New feature or request label Sep 4, 2023
@evelynhathaway evelynhathaway moved this to In Progress in 🖼️ Pixel Sort Sep 5, 2023
@evelynhathaway evelynhathaway self-assigned this Sep 5, 2023
@evelynhathaway
Copy link
Owner Author

Surprisingly, the refactor alone isn't consistently that much more performant; however this refactor could allow for a small uplift if the intervals were sorted while making them.

image

Code for push slices
export const makeIntervals = (pixels: Pixels1D, thresholds: Array<Threshold>): Array<Interval> => {
	const intervals: Array<Interval> = [];
	let startingIndex = 0;
	let didMeetThreshold: boolean | undefined;
	for (let index = 0; index < pixels.length; index++) {
		const pixel = pixels[index];
		const meetsThreshold = checkThresholds(pixel, thresholds);
		const isLastPixel = index === pixels.length - 1;
		if (didMeetThreshold == null) didMeetThreshold = meetsThreshold;
		if (meetsThreshold !== didMeetThreshold || isLastPixel) {
			intervals.push({
				sort: didMeetThreshold,
				pixels: pixels.slice(startingIndex, index),
			});
			startingIndex = index;
			didMeetThreshold = meetsThreshold;
		}
	}
	return intervals;
};
Code for push single unsorted
export const makeIntervals = (pixels: Pixels1D, thresholds: Array<Threshold>): Array<Interval> => {
	const intervals: Array<Interval> = [];
	let didMeetThreshold: boolean | undefined;
	for (const pixel of pixels) {
		const meetsThreshold = checkThresholds(pixel, thresholds);
		if (meetsThreshold === didMeetThreshold) {
			intervals.at(-1)?.pixels.push(pixel);
		} else {
			intervals.push({
				sort: meetsThreshold,
				pixels: [pixel],
			});
			didMeetThreshold = meetsThreshold;
		}
	}
	return intervals;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

No branches or pull requests

1 participant