2018년 1월 25일 목요일

HTML - 10 Ways to Minimize Reflows and Improve Performance



HTML & CSS - 10 Ways to Minimize Reflows and Improve Performance


Despite web pages reaching 2MB performance remains a hot topic. The slicker your application, the better the user experience and the higher the conversion rate!
That said, I’m guilty of adding superficial CSS3 animations or manipulating multiple DOM elements without considering the consequences. Two terms are used in the browser world when visual affects are applied:
Repaints
A repaint occurs when changes are made to elements that affect visibility but not the layout. For example, opacitybackground-colorvisibility, and outline. Repaints are expensive because the browser must check the visibility of all other nodes in the DOM — one or more may have become visible beneath the changed element.
Reflows
Reflows have a bigger impact. This refers to the re-calculation of positions and dimensions of all elements, which leads to re-rendering part or all of the document. Changing a single element can affect all children, ancestors, and siblings.
Both are browser-blocking; neither the user or your application can perform other tasks during the time that a repaint or reflow occurring. In extreme cases, a CSS effect could lead to slower JavaScript execution. This is one of the reasons you encounter issues such as jerky scrolling and unresponsive interfaces.
It’s useful to understand when reflows are triggered:
Adding, removing or changing visible DOM elements
The first is obvious; using JavaScript to change the DOM will cause a reflow.
Adding, removing or changing CSS styles
Similarly, directly applying CSS styles or changing the class may alter the layout. Changing the width of an element can affect all elements on the same DOM branch and those surrounding it.
CSS3 animations and transitions
Every frame of the animation will cause a reflow.
Using offsetWidth and offsetHeight
Bizarrely, reading an element’s offsetWidth and offsetHeight property can trigger an initial reflow so the figures can be calculated.
User actions
Finally, the user can trigger reflows by activating a :hover effect, entering text in a field, resizing the window, changing the font dimensions, switching stylesheets or fonts.
The reflow processing flow hit will vary. Some browsers are better than others at certain operations. Some elements are more expensive to render than others. Fortunately, there are several general tips you can use to enhance performance.

1. Use Best-Practice Layout Techniques

I can’t believe I need to say this in 2015 but don’t use inline styles or tables for layout!
An inline style will affect layout as the HTML is downloaded and trigger an additional reflow. Tables are expensive because the parser requires more than one pass to calculate cell dimensions. Using table-layout: fixed can help when presenting tabular data since column widths are based on the header row content.
Using flexbox for your main page layout can also have a performance hit because the position and dimensions of flex items can change as the HTML is downloaded.

2. Minimize the Number of CSS Rules

The fewer rules you use, the quicker the reflow. You should also avoid complex CSS selectors where possible.
This can be especially problematic if you’re using a framework such as Bootstrap — few sites use more than a fraction of the styles provided. Tools like Unused CSSuCSSgrunt-uncss, and gulp-uncss can significantly reduce your style definitions and file sizes.

3. Minimize DOM depths

Slightly trickier — reduce the size of your DOM tree and the number of elements in each branch. The smaller and shallower your document, the quicker it can be reflowed. It may be possible to remove unnecessary wrapper elements if you’re not supporting older browsers.

4. Update Classes Low in the DOM Tree

Make class changes on elements as low in the DOM tree as possible (i.e. elements that don’t have multiple deeply nested children). This can limit the scope of the reflow to as few nodes as necessary. In essence, only apply class changes to parent nodes such as wrappers if the effect on nested children is minimal.

5. Remove Complex Animations From the Flow

Ensure animations apply to a single element by removing them from the document flow with position: absolute; or position: fixed;. This permits the dimensions and position to be modified without affecting other elements in the document.

6. Modify Hidden Elements

Elements hidden with display: none; will not cause a repaint or reflow when they are changed. If practical, make changes to the element before making it visible.

7. Update Elements in Batch

Performance can be improved by updating all DOM elements in a single operation. This simple example causes three reflows:
var myelement = document.getElementById('myelement');
myelement.width = '100px';
myelement.height = '200px';
myelement.style.margin = '10px';
We can reduce this to a single reflow which is also easier to maintain, e.g.
var myelement = document.getElementById('myelement');
myelement.classList.add('newstyles');
.newstyles {
 width: 100px;
 height: 200px;
 margin: 10px;
}
You can also minimize the times you need to touch the DOM. Let’s assume you wanted to create this bullet list:
  • item 1
  • item 2
  • item 3
Adding each element one at a time causes up to seven reflows — one when the <ul>is appended, three for each <li> and three for the text. However, a single reflow can be implemented using a DOM fragment and building the nodes in memory first, e.g.
var
 i, li,
 frag = document.createDocumentFragment(),
 ul = frag.appendChild(document.createElement('ul'));

for (i = 1; i <= 3; i++) {
 li = ul.appendChild(document.createElement('li'));
 li.textContent = 'item ' + i;
}

document.body.appendChild(frag);

8. Limit the Affected Elements

Avoid situations where a large number of elements could be affected. Consider a tabbed content control where clicking a tab activates a different content block. The surrounding elements would be affected if each content block had a different height. You may be able to improve performance by setting a fixed height for the container or removing the control from the document flow.

9. Recognize that Smoothness Compromises Performance

Moving an element one pixel at a time may look smooth but slower devices can struggle. Moving the element by four pixels per frame requires one quarter of the reflow processing and may only be slightly less smooth.

10. Analyze Repaint Issues with Browser Tools

All mainstream browsers provide developer tools that highlight how reflows affect performance. In Blink/Webkit browsers such as Chrome, Safari, and Opera, open the Timeline panel and record an activity:
Chrome timeline tool
A similar Timeline panel is available in the Firefox Developer Tools:
Firefox timeline tool
The panel is named UI Responsiveness in the Internet Explorer F12 Developer Tools:
IE UI Responsiveness tool
All browsers display reflow and repainting times in green. The tests above were simple examples not involving significant animation yet layout rendering requires more time than other factors such as scripting. Reduce your reflows and better performance will follow.
If you’ve had success in improving performance in your animations and UIs using these or other suggestions, let us know in the comments.


출처 : https://www.sitepoint.com/10-ways-minimize-reflows-improve-performance/





정보 - AI, 머신러닝, 딥러닝




‘머신러닝-딥러닝’, 뭐가 다를까



지난해 ‘알파고 쇼크’ 이후 인공지능(AI)에 대한 관심이 커진 가운데, 자주 언급되는 AI 관련 용어가 바로 ‘기계학습’(머신러닝)과 ‘심층학습’(딥러닝)이다.
하지만 머신러닝과 딥러닝의 차이를 잘 모르거나, 혼동하는 사람이 적지 않다. 이에 두 기술 용어의 차이를 공학용 소프트웨어 ‘MATLAB’의 개발원인 매스웍스가 쉽게 해설한 유튜브 동영상이 관심을 모으고 있다.
머신러닝이나 딥러닝 모두 학습 모델을 제공해 데이터를 분류하는 데 사용되는 기술이다. 그 기능을 설명하는 데 있어 자주 사용되는 것이 개와 고양이 이미지를 분류하는 예시인데, 매스웍스가 공개한 동영상에서도 이 같은 방법을 들어 머신러닝과 딥러닝을 설명한다.
사람이 개와 고양이 이미지를 봤을 때 판별할 수 있는 것은 해당 이미지를 뇌에서 분석하고 특징을 확정 지은 뒤, 지식과 경험에 따라 판단하는 능력이 갖춰져 있기 때문이다.
이 같은 일을 컴퓨터로 처리하는 것이 머신러닝과 딥러닝의 목적이다. 하지만 둘 사이에는 접근의 차이가 존재한다.
머신러닝의 경우 주어진 소재를 우선 인간이 먼저 처리한다. 이미지의 경우 사람이 트레이닝 데이터를 알맞게 분류하는 등 컴퓨터가 인식할 수 있도록 해야 한다. 그 다음 컴퓨터가 데이터에 포함된 특징을 분석하고 축적하는 과정을 거친다. 마지막으로 컴퓨터가 축적된 데이터를 바탕으로 이미지의 특징을 종합해 답을 이끌어 내는 것이 머신러닝이다.
물체를 인식할 때나 정경을 인식해 물체를 검출할 때 머신러닝 기술을 사용할 수 있다.
즉 기계학습의 흐름을 보면 각 그림의 특징을 기계(컴퓨터)에 인식시키고 학습시킴으로써 문제를 해결하는 것이다.
딥러닝의 경우 인간이 하던 작업이 생략된다. 바탕이 되는 데이터를 그대로 주고, 딥러닝 알고리즘이 회선 신경망(Convolutional Neural Network, CNN)을 이용해 스스로 분석한 후 답을 내는 방식이 딥러닝이다.
CNN은 이미지를 이해하고 이로부터 높은 수준의 추상화된 정보를 추출하거나, 새로운 질감을 가진 그림을 그리는 등 다양한 영상처리, 컴퓨터 비전 분야에서 많이 연구되고 있는 인공신경망의 한 종류.
여기서 알아야 할 점은 딥러닝은 넓은 의미에서 머신러닝 개념에 포함된다는 것이다.
회선 신경망 작동 원리.
회선 신경망 작동 원리.
그렇다면 머신러닝과 딥러닝은 어떻게 다르게 사용될까.
딥러닝은 방대한 양의 데이터에 대한 계산 처리를 위해 연산 능력이 높은 GPU 등의 하드웨어가 필요하다. 따라서 하드웨어 영향이 있고 데이터양이 많은 경우에는 딥러닝을, 그렇지 않은 경우에는 머신러닝을 채용하는 것이 좋다.
머신러닝을 선택한 경우 학습 시킬 때 수많은 등급 분류를 사용할 수 있다는 옵션이 있다. 또 최적의 답을 도출하기 위해 어떤 특징을 이용하거나 하는 등의 선택을 할 수도 있다. 머신러닝의 경우 목적에 따라 접근 방식을 달리 선택할 수 있는 장점도 있다.
우수한 하드웨어와 많은 양의 데이터가 있다면 딥러닝을, 그렇지 못하다면 머신러닝을 선택하는 것이 좋다.
우수한 하드웨어와 많은 양의 데이터가 있다면 딥러닝을, 그렇지 못하다면 머신러닝을 선택하는 것이 좋다.
머신러닝의 경우는 자신의 연구를 포함시킬 여지가 남아 있고 처리 시간이 짧은 반면, 딥러닝은 이용자의 많은 지식과 노력 없이도 높은 정밀도를 얻을 수 있는 특징이 있다.
그러나 딥러닝의 경우 높은 시스템 성능이 요구되고 처리 시간이 오래 걸린다는 단점이 있다. 또 딥러닝을 높은 정밀도로 학습하기 위해서는 방대한 데이터가 필요할 뿐만 아니라, 내부의 알고리즘을 인간이 이해할 수 없기 때문에 오류수정(디버깅)이 사실상 불가능 하다.
즉 머신러닝과 딥러닝은 엄격하게 같은 머신러닝에 포함돼 있는 것이며 그 구분은 처리하는 데이터의 양, 하드웨어 성능, 그리고 구하고 싶은 결과의 내용에 따라 선택해야 하는 것이다.(▶관련동영상 보기)
머신러닝과 딥러닝 비교.
머신러닝과 딥러닝 비교.
딥러닝은 머신러닝 개념에 포함된다.
딥러닝은 머신러닝 개념에 포함된다.


출처 : http://www.zdnet.co.kr/news/news_view.asp?artice_id=20170807140504



2018년 1월 18일 목요일

C# - DocumentFormat.OpenXml




private void ExportDataSet(DataSet ds, string destination)
        {
            using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
            {
                var workbookPart = workbook.AddWorkbookPart();

                workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

                workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

                foreach (System.Data.DataTable table in ds.Tables) {

                    var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
                    var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
                    sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

                    DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
                    string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

                    uint sheetId = 1;
                    if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
                    {
                        sheetId =
                            sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                    }

                    DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
                    sheets.Append(sheet);

                    DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

                    List<String> columns = new List<string>();
                    foreach (System.Data.DataColumn column in table.Columns) {
                        columns.Add(column.ColumnName);

                        DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                        cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                        cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                        headerRow.AppendChild(cell);
                    }


                    sheetData.AppendChild(headerRow);

                    foreach (System.Data.DataRow dsrow in table.Rows)
                    {
                        DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                        foreach (String col in columns)
                        {
                            DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                            cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                            cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                            newRow.AppendChild(cell);
                        }

                        sheetData.AppendChild(newRow);
                    }

                }
            }
        }


private void ExportDSToExcel(DataSet ds, string destination)
{
    using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
    {
        var workbookPart = workbook.AddWorkbookPart();
        workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
        workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

        uint sheetId = 1;

        foreach (DataTable table in ds.Tables)
        {
            var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
            var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
            sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);                

            DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
            string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

            if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
            {
                sheetId =
                    sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
            sheets.Append(sheet);

            DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

            List<String> columns = new List<string>();
            foreach (DataColumn column in table.Columns)
            {
                columns.Add(column.ColumnName);

                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                headerRow.AppendChild(cell);
            }

            sheetData.AppendChild(headerRow);

            foreach (DataRow dsrow in table.Rows)
            {
                DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                foreach (String col in columns)
                {
                    DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                    cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                    cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }
        }
    }
}


출처 : https://code.i-harness.com/ko/q/b43947



javascript - SQL 예약어 제거

  <script language="javascript"> //특수문자, 특정문자열(sql예약어) 제거 function checkSearchedWord(obj){ obj.value = obj.value+&quo...